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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing XML with vbscript 1

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I have some XML that is similar to the following:

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?> 
- <audit>
  <auditdate>19/05/2011</auditdate> 
  <audittime>14:39:26</audittime> 
  <hostname>PC211</hostname> 
- <apps>
- <item>
  <appname>Vuze</appname> 
  <installdate /> 
  <version>4.</version> 
  <knownapp>False</knownapp> 
  </item>
- <item>
  <appname>WebEx</appname> 
  <installdate /> 
  <version>.</version> 
  <knownapp>False</knownapp> 
  </item>
</apps>
<previousapps>
- <item>
  <installationid>123</installationid>
  <appname>Microsoft Word</appname> 
  <installdate /> 
  <version>1</version> 
  <knownapp>True</knownapp> 
  </item>
</previousapps>

[/coder]

I want to look at each <item> contained within the branch <apps> and then if <knownapp> is true do A and if <knownapp> is false do B.

After all of them are done I then want to look at each item in the <previousapps> branch.  I want to say if there is no <appname> in the <apps> branch that matches the <appname> in the <previousapps> branch then get the <installationid> and do something with that.

Hope this sort of makes sense - I am really struggling to find a decent article on parsing XML in vbscript so any pointers would be be very much appreciated.

Thanks very much

Ed
 
Hi emozley,

To help you start, here is an simplest example how to parse an XML-document:

parse_xml.vbs
Code:
[COLOR=#804040][b]option[/b][/color] [COLOR=#804040][b]explicit[/b][/color]

[COLOR=#804040][b]dim[/b][/color] xmlDoc[COLOR=#804040][b],[/b][/color] objNodeList

[COLOR=#804040][b]set[/b][/color] xmlDoc [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]CreateObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"MSXML2.DOMDocument"[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#0000ff]'set xmlDoc = CreateObject("Microsoft.XMLDOM")[/color]

xmlDoc[COLOR=#804040][b].[/b][/color][COLOR=#804040][b]load[/b][/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"obce.xml"[/color][COLOR=#804040][b])[/b][/color]

[COLOR=#804040][b]set[/b][/color] objNodeList [COLOR=#804040][b]=[/b][/color] xmlDoc[COLOR=#804040][b].[/b][/color]getElementsByTagName[COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"OBEC"[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#0000ff]' Create list of nodes OBEC, which have this form[/color]
[COLOR=#0000ff]' <OBEC>[/color]
[COLOR=#0000ff]'   <NAZOV>..</NAZOV>[/color]
[COLOR=#0000ff]'   <PSC>..</PSC>[/color]
[COLOR=#0000ff]'   <POSTA>..</POSTA>[/color]
[COLOR=#0000ff]'   <OKRES>..</OKRES>[/color]
[COLOR=#0000ff]' </OBEC>[/color]

[COLOR=#804040][b]if[/b][/color] objNodeList[COLOR=#804040][b].[/b][/color]length [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color] [COLOR=#804040][b]then[/b][/color]
  [COLOR=#804040][b]dim[/b][/color] fso[COLOR=#804040][b],[/b][/color] csv_file_name[COLOR=#804040][b],[/b][/color] csv_file[COLOR=#804040][b],[/b][/color] csv_line
  WScript[COLOR=#804040][b].[/b][/color]Echo[COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Parsing XML and writing data to CSV-file ..."[/color][COLOR=#804040][b])[/b][/color]  
 [COLOR=#0000ff] ' Open CSV file[/color]
  csv_file_name [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"obce.csv"[/color] 
  [COLOR=#804040][b]set[/b][/color] fso [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]CreateObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Scripting.FileSystemObject"[/color][COLOR=#804040][b])[/b][/color]
  [COLOR=#804040][b]const[/b][/color] ForReading [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color][COLOR=#804040][b],[/b][/color] ForWriting [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]2[/color][COLOR=#804040][b],[/b][/color] ForAppending [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]8[/color] 
  [COLOR=#804040][b]set[/b][/color] csv_file [COLOR=#804040][b]=[/b][/color] fso[COLOR=#804040][b].[/b][/color]OpenTextFile[COLOR=#804040][b]([/b][/color]csv_file_name[COLOR=#804040][b],[/b][/color] ForWriting[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]True[/color][COLOR=#804040][b])[/b][/color]
 [COLOR=#0000ff] ' create CSV-header-line[/color]
  csv_line [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"Názov obce;PS?;Dodacia pošta;Okres"[/color]
  csv_file[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]writeline[/color][COLOR=#804040][b]([/b][/color]csv_line[COLOR=#804040][b])[/b][/color]

  [COLOR=#804040][b]dim[/b][/color] obec[COLOR=#804040][b],[/b][/color] child[COLOR=#804040][b],[/b][/color] nazov[COLOR=#804040][b],[/b][/color] psc[COLOR=#804040][b],[/b][/color] posta[COLOR=#804040][b],[/b][/color] okres

  [COLOR=#804040][b]for[/b][/color] [COLOR=#804040][b]each[/b][/color] obec [COLOR=#804040][b]in[/b][/color] objNodeList
   [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] obec[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]"NAZOV"[/color]
          nazov [COLOR=#804040][b]=[/b][/color] child[COLOR=#804040][b].[/b][/color]Text
        [COLOR=#804040][b]case[/b][/color] [COLOR=#ff00ff]"PSC"[/color]
          psc [COLOR=#804040][b]=[/b][/color] child[COLOR=#804040][b].[/b][/color]Text
        [COLOR=#804040][b]case[/b][/color] [COLOR=#ff00ff]"POSTA"[/color]
          posta [COLOR=#804040][b]=[/b][/color] child[COLOR=#804040][b].[/b][/color]Text
        [COLOR=#804040][b]case[/b][/color] [COLOR=#ff00ff]"OKRES"[/color]
          okres [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] ' compose CSV-line[/color]
     csv_line [COLOR=#804040][b]=[/b][/color] nazov [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]";"[/color] [COLOR=#804040][b]&[/b][/color] psc [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]";"[/color] [COLOR=#804040][b]&[/b][/color] posta [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]";"[/color] [COLOR=#804040][b]&[/b][/color] okres
     csv_file[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]writeline[/color][COLOR=#804040][b]([/b][/color]csv_line[COLOR=#804040][b])[/b][/color]
  [COLOR=#804040][b]next[/b][/color]
 [COLOR=#0000ff] ' close CSV-file and release object from memory[/color]
  csv_file[COLOR=#804040][b].[/b][/color][COLOR=#804040][b]Close[/b][/color]
  [COLOR=#804040][b]set[/b][/color] csv_file [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]nothing[/b][/color]
  WScript[COLOR=#804040][b].[/b][/color]Echo[COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"..done."[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#804040][b]else[/b][/color]
  [COLOR=#804040][b]dim[/b][/color] err_msg
  err_msg [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]chr[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]34[/color][COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"OBEC"[/color] [COLOR=#804040][b]&[/b][/color] [COLOR=#008080]chr[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]34[/color][COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" tag not found !"[/color]
  WScript[COLOR=#804040][b].[/b][/color]Echo[COLOR=#804040][b]([/b][/color]err_msg[COLOR=#804040][b])[/b][/color]
[COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]

[COLOR=#0000ff]' At end release XMLDOM object from memory[/color]
[COLOR=#804040][b]set[/b][/color] xmlDoc [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]nothing[/b][/color]

The script above parses XML document of this form
obce.xml
Code:
<?xml version="1.0" encoding="windows-1250"?>
<OBCE>
  <OBEC>
    <NAZOV>Ábelová</NAZOV>
    <PSC>985 13</PSC>
    <POSTA>Ábelová</POSTA>
    <OKRES>Lu?enec</OKRES>
  </OBEC>
  <OBEC>
    <NAZOV>?á?ov</NAZOV>
    <PSC>905 01</PSC>
    <POSTA>Senica 1</POSTA>
    <OKRES>Senica</OKRES>
    </OBEC>
  <OBEC>
    <NAZOV>?ápor</NAZOV>
    <PSC>951 17</PSC>
    <POSTA>Cabaj- ?ápor</POSTA>
    <OKRES>Nitra</OKRES>
  </OBEC>
  ...

  ...
</OBCE>
and writes the output to a CSV-file of this form
obce.csv
Code:
Názov obce;PS?;Dodacia pošta;Okres
Ábelová;985 13;Ábelová;Lu?enec
?á?ov;905 01;Senica 1;Senica
?ápor;951 17;Cabaj- ?ápor;Nitra
...

...

The philosophy of parsing your XML-document would be analogical.
 
Hi Mikrom,

This is fantastic - thank you very much.

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top