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

Parse Tricky XML File 1

Status
Not open for further replies.

zmcanally

Technical User
Jun 12, 2002
26
US
I am working on an auditing project which needs to report on data that is in an XML file. I would like to load in the XML file and write it to a database (probably access) and report off of the database. Any help woulf be appreciated

Zack

'// begin sample XML data
<ComputerList>
<Computer Name=&quot;105LAB&quot; QueryState=&quot;Queried&quot; Username=&quot;&quot;>
<ManagedObject Name=&quot;WMI: Logged on user&quot; Query=&quot;select UserName from Win32_ComputerSystem&quot;>
<Instance>
<Attribute Name=&quot;UserName&quot; Value=&quot;INGR-IMGS\rray&quot; />
</Instance>
</ManagedObject>
<ManagedObject Name=&quot;WMI: Operating System&quot; Query=&quot;Select Caption from Win32_OperatingSystem&quot;>
<Instance>
<Attribute Name=&quot;Caption&quot; Value=&quot;Microsoft Windows XP Professional&quot; />
</Instance>
</ManagedObject>
<ManagedObject Name=&quot;WMI: Applications installed by Windows Installer&quot; Query=&quot;select Name from win32_product&quot;>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;WebFldrs XP&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;Microsoft Windows Journal Viewer&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;Adobe Reader 6.0&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;Microsoft Office 2000 Standard&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;Microsoft .NET Framework 1.1&quot; />
</Instance>
</ManagedObject>
</Computer>
<Computer Name=&quot;ABARBAREE&quot; QueryState=&quot;Queried&quot; Username=&quot;&quot;>
<ManagedObject Name=&quot;WMI: Logged on user&quot; Query=&quot;select UserName from Win32_ComputerSystem&quot;>
<Instance>
<Attribute Name=&quot;UserName&quot; Value=&quot;INTERGRAPH\ahbarbar&quot; />
</Instance>
</ManagedObject>
<ManagedObject Name=&quot;WMI: Operating System&quot; Query=&quot;Select Caption from Win32_OperatingSystem&quot;>
<Instance>
<Attribute Name=&quot;Caption&quot; Value=&quot;Microsoft Windows 2000 Professional&quot; />
</Instance>
</ManagedObject>
<ManagedObject Name=&quot;WMI: Applications installed by Windows Installer&quot; Query=&quot;select Name from win32_product&quot;>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;DiskeeperWorkstation&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;Microsoft Project 2000&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;Microsoft Office 2000 Professional&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;Nauticos Renavigation Suite V2003.02&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;GeoMedia Professional&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;WebFldrs&quot; />
</Instance>
</ManagedObject>
</Computer>
<Computer Name=&quot;ABARBARLT&quot; QueryState=&quot;Queried&quot; Username=&quot;&quot;>
<ManagedObject Name=&quot;WMI: Logged on user&quot; Query=&quot;select UserName from Win32_ComputerSystem&quot;>
<Instance>
<Attribute Name=&quot;UserName&quot; Value=&quot;&quot; />
</Instance>
</ManagedObject>
<ManagedObject Name=&quot;WMI: Operating System&quot; Query=&quot;Select Caption from Win32_OperatingSystem&quot;>
<Instance>
<Attribute Name=&quot;Caption&quot; Value=&quot;Microsoft Windows 2000 Professional&quot; />
</Instance>
</ManagedObject>
<ManagedObject Name=&quot;WMI: Applications installed by Windows Installer&quot; Query=&quot;select Name from win32_product&quot;>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;ActivePerl 5.6.1 Build 631&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;Microsoft Project 2000&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;Microsoft Office 2000 Professional&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;Microsoft Visio Professional 2002 SR-1 [English]&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;GeoMedia Professional&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;WebFldrs&quot; />
</Instance>
<Instance>
<Attribute Name=&quot;Name&quot; Value=&quot;CaliberRM&quot; />
</Instance>
</ManagedObject>
</Computer>
</ComputerList>
'// End sample XML data
 

Try the XML object for retrieving this information out of your XML files. To see if you have it on your machine look at Project>References.

BTW zmcanally have you read FAQ222-2244?

Good Luck

 
That's not that tricky -- it just nests to 4 levels, not bad at all.

vb5prgrmr is correct - to parse this you would need to have the Microsoft XML parser referenced (MSXML 4.0, which is now at SP2). If the file/data is less than a megabyte, you can load it into a DOMDocument40 object. Larger than that, you'll need to process it sequentially using the SAX parser.

To load it into a DOM, you do something like this:
Code:
  Dim oDOM as MSXML3.DOMDocument40
  Set oDOM = New MSXML2.DOMDocument40

  oDOM.Load(&quot;C:\MyXMLFile.xml&quot;)
Once it's in the DOM, you use the SelectSingleNode method to retrieve MSXML2.IXMLDOMElement objects for the elements, and for elements that have attributes, use the Attributes collection to look at them.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks for your help guys - The only XML files I have worked with thus far are dumped from recordsets and fiarly easy to handle. I began using the MSXML component, but keep running into the following error: Runtime Error: Expected Token 'EOF' found 'NAME' Computer -->Name<--
Here is the code I'm testing with:
Code:
    Dim oDom As MSXML2.DOMDocument40
    Dim oNode As IXMLDOMNode    
    Set oDom = New MSXML2.DOMDocument40
    oDom.Load (App.Path & &quot;\SmallerXML.xml&quot;)
    Set oNode = oDom.documentElement.selectSingleNode(&quot;Computer Name&quot;)
    MsgBox oNode.Text

Thanks,

Zack

 
Spaces act as separators.
&quot;Computer&quot; is the name of the element. &quot;Name&quot; is the name of an attribute attached to that element. So to access the Name attribute, you'd do something like:
Code:
Dim oAttr as MSXML2.ISXMLDOMAttribute

Set oNode = oDom.documentElement.selectSingleNode(&quot;Computer&quot;)

Set oAttr = oNode.getAttribute(&quot;Name&quot;)
If Not oAttr Is Nothing Then
  sName = oAttr.Text
End If
Take a look at:

Chip H.





If you want to get the best response to a question, please check out FAQ222-2244 first
 
Chip - thanks for the help &amp; link - - I got it all working now. That space was the culprit

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top