Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to find duplicate xml elements in vb6 and remove the duplicates 1

Status
Not open for further replies.

Vani2020

Programmer
Jan 3, 2020
1
US
how to find duplicate xml elements in vb6 and remove the duplicates
Before:

<querys>
<query>
<number>1</number>
<data>4256</data>
</query>
<query>
<number>2</number>
<data>8986</data>
</query>
<query>
<number>1</number>
<data>4256</data>
</query>
</querys>

After:

<querys>
<query>
<number>1</number>
<data>4256</data>
</query>
<query>
<number>2</number>
<data>8986</data>
</query>
</querys>
 
Hi Vani2020,
I would use XMLDOM and Dictionary, i.e.:
Code:
create query-existence-dictionary
For every query in queries do:
  parse number and data and create compound key = number_data
  if there is no entry in the dictionary with this key then
    mark the existence of query in dictionary
  if there is already such key in dictionary then
    remove the query element from queries
 
Unfortunately I don't have VB6, but here is in VBscript a proof of concept of the approach I mentioned above.
You can try it out and it won't be difficult to implement it in VB6.

the script: vani2020.vbs
Code:
DBG_INFO [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]true[/color][COLOR=#0000ff] ' setting: true / false[/color]

[COLOR=#0000ff]' create Dictionary object[/color]
[COLOR=#804040][b]set[/b][/color] query_dictionary [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]CreateObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Scripting.Dictionary"[/color][COLOR=#804040][b])[/b][/color]

[COLOR=#0000ff]' create XMLDOM object[/color]
[COLOR=#804040][b]set[/b][/color] xml_doc [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]CreateObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Microsoft.XMLDOM"[/color][COLOR=#804040][b])[/b][/color]

xml_doc[COLOR=#804040][b].[/b][/color][COLOR=#804040][b]load[/b][/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"vani2020.xml"[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#804040][b]set[/b][/color] queries [COLOR=#804040][b]=[/b][/color] xml_doc[COLOR=#804040][b].[/b][/color]getElementsByTagName[COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"query"[/color][COLOR=#804040][b])[/b][/color]

[COLOR=#804040][b]for[/b][/color] [COLOR=#804040][b]each[/b][/color] query [COLOR=#804040][b]in[/b][/color] queries
 [COLOR=#0000ff] ' parse each elements childs[/color]
  [COLOR=#804040][b]for[/b][/color] [COLOR=#804040][b]each[/b][/color] child [COLOR=#804040][b]in[/b][/color] query[COLOR=#804040][b].[/b][/color]ChildNodes
    [COLOR=#804040][b]select[/b][/color] [COLOR=#804040][b]case[/b][/color] child[COLOR=#804040][b].[/b][/color]NodeName
      [COLOR=#804040][b]case[/b][/color] [COLOR=#ff00ff]"number"[/color]
        number [COLOR=#804040][b]=[/b][/color] child[COLOR=#804040][b].[/b][/color]Text
      [COLOR=#804040][b]case[/b][/color] [COLOR=#ff00ff]"data"[/color]
        data [COLOR=#804040][b]=[/b][/color] child[COLOR=#804040][b].[/b][/color]Text
    [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]select[/b][/color]
  [COLOR=#804040][b]next[/b][/color]
 [COLOR=#0000ff] ' create dictionary key[/color]
  query_key [COLOR=#804040][b]=[/b][/color] number [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"_"[/color] [COLOR=#804040][b]&[/b][/color] data
  [COLOR=#804040][b]if[/b][/color] DBG_INFO [COLOR=#804040][b]then[/b][/color] 
    wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"creating query key: "[/color] [COLOR=#804040][b]&[/b][/color] query_key
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
  [COLOR=#804040][b]if[/b][/color] [COLOR=#804040][b]not[/b][/color] query_dictionary[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]exists[/color][COLOR=#804040][b]([/b][/color]query_key[COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]then[/b][/color]
   [COLOR=#0000ff] ' add to dictionary[/color]
    query_dictionary[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]add[/color] query_key[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"x"[/color]
    [COLOR=#804040][b]if[/b][/color] DBG_INFO [COLOR=#804040][b]then[/b][/color] 
      wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"adding to dictionary: "[/color] [COLOR=#804040][b]&[/b][/color] query_key
    [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
  [COLOR=#804040][b]else[/b][/color]
   [COLOR=#0000ff] ' query_key exists alredy in dictionary - duplicity found ![/color]
    [COLOR=#804040][b]if[/b][/color] DBG_INFO [COLOR=#804040][b]then[/b][/color] 
      wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"* duplicity found: key = "[/color] [COLOR=#804040][b]&[/b][/color] query_key
      wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"  .. removing query element from XML"[/color]
    [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
   [COLOR=#0000ff] ' remove query element from queries[/color]
    query[COLOR=#804040][b].[/b][/color]parentNode[COLOR=#804040][b].[/b][/color]removeChild[COLOR=#804040][b]([/b][/color]query[COLOR=#804040][b])[/b][/color]
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
  [COLOR=#804040][b]if[/b][/color] DBG_INFO [COLOR=#804040][b]then[/b][/color] wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]""[/color] [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
[COLOR=#804040][b]next[/b][/color]

[COLOR=#804040][b]if[/b][/color] DBG_INFO [COLOR=#804040][b]then[/b][/color] wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"Done."[/color] [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
  
[COLOR=#0000ff]' at end[/color]
[COLOR=#0000ff]' write resulting XML document[/color]
xml_doc[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]save[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"vani2020_result.xml"[/color][COLOR=#804040][b])[/b][/color]

[COLOR=#0000ff]' release objects from memory[/color]
[COLOR=#804040][b]set[/b][/color] xml_doc [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]nothing[/b][/color]
[COLOR=#804040][b]set[/b][/color] query_dictionary [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]nothing[/b][/color]

Running the script:
Code:
c:\>cscript /NoLogo vani2020.vbs
creating query key: 1_4256
adding to dictionary: 1_4256

creating query key: 2_8986
adding to dictionary: 2_8986

creating query key: 1_4256
* duplicity found: key = 1_4256
  .. removing query element from XML

Done.

input XML file: vani2020.xml
Code:
<querys>
  <query>
    <number>1</number>
    <data>4256</data>
  </query>
  <query>
    <number>2</number>
    <data>8986</data>
  </query>
  <query>
    <number>1</number>
    <data>4256</data>
  </query>
</querys>

output XML file: vani2020_result.xml
Code:
<querys>
	<query>
		<number>1</number>
		<data>4256</data>
	</query>
	<query>
		<number>2</number>
		<data>8986</data>
	</query>
</querys>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top