1

Thanks for an AWESOME forum it truly is a wealth of knowledge and hopefully in searching for a solution I can help someone else.

I wonder if some kind soul could help me by suggesting how to or possibly pointing me in the right direction with a code example to parse the xml code shown here. I have been searching all over the web for a vbscript example but am really battling to find one that solves the problem.

Most of the examples on the web show how to extract certain tags or text matches in nodes or child nodes and don't quite explain hot to reference the portions I am interested in in this code.

I already have python code which works fine but the vbscript has me stumped :-(

The xmlDOC defines a vAPP's environment settings, the parts I am interested in are: oe:key and oe:value for each of the node under <PropertySection>.

I need to have the key and the value stored in a text file with entries like

bootflag=a
clusetername=cluster test

etc. Here is the XML doc:

<Environment xmlns="http://schemas.dmtf.org/ovf/environment/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oe="http://schemas.dmtf.org/ovf/environment/1" xmlns:ve="http://www.vmware.com/schema/ovfenv" oe:id="" ve:vCenterId="vm-167">
 <PlatformSection>
  <Kind>VMware ESXi</Kind>
  <Version>5.5.0</Version>
  <Vendor>VMware, Inc.</Vendor>
  <Locale>en</Locale>
 </PlatformSection>
 <PropertySection>
  <Property oe:key="bootflag" oe:value="A"/>
  <Property oe:key="clustername" oe:value="custer test"/>
  <Property oe:key="datacenter_name" oe:value="datacenter test"/>
  <Property oe:key="dns1" oe:value="192.168.1.198"/>
  <Property oe:key="domain" oe:value="zen.com"/>
  <Property oe:key="gateway" oe:value="192.168.1.1"/>
  <Property oe:key="hostname" oe:value="rambo1"/>
  <Property oe:key="ip" oe:value="192.168.1.104"/>
  <Property oe:key="netmask" oe:value="255.255.255.0"/>
  <Property oe:key="vcenter_password" oe:value="vpass"/>
  <Property oe:key="vcenterip" oe:value="1.2.3.4"/>
  <Property oe:key="vcenteruser" oe:value="vcenteruser"/>
 </PropertySection>
 <ve:EthernetAdapterSection>
  <ve:Adapter ve:mac="00:50:56:88:62:8a" ve:network="VM Network" ve:unitNumber="7"/>
  <ve:Adapter ve:mac="00:50:56:88:46:25" ve:network="VM Network" ve:unitNumber="8"/>
  <ve:Adapter ve:mac="00:50:56:88:31:59" ve:network="VM Network" ve:unitNumber="9"/>
  <ve:Adapter ve:mac="00:50:56:88:5e:00" ve:network="VM Network" ve:unitNumber="10"/>
 </ve:EthernetAdapterSection>
</Environment>

I would really appreciate any help.


EDIT: Based on the reply below I tried this bit of code but I'm sure I'm going wrong with filling in the node info, specifically "oe:key" and "oe:value".

Please find the update code below:

Set objDoc = CreateObject("MSXML.DOMDocument")
objDoc.Load ("c:\Temp\ovfenv.xml")
' Iterate over all elements contained in the <root> element:
Set objRoot = objDoc.documentElement

s = ""
t = ""
For Each child in objRoot.childNodes
   s = s & child.getAttribute("oe:key") & " "
   t = t & child.getAttribute("oe:value") & " "
Next
MsgBox s    
MsgBox t    

' Find a particular element using XPath:
Set objNode = objDoc.selectSingleNode("/PropertySection/Property[@oe:key='bootflag']")
MsgBox objNode.getAttribute("oe:value")
Tomalak
  • 332,285
  • 67
  • 532
  • 628
user3407537
  • 51
  • 1
  • 13

2 Answers2

2

You almost had it.

You must - in MSXML just like in every other API implementation - declare any namespaces you intend to use in XPath.

Option Explicit  ' that's always a good idea, too!
Dim doc, ns, node

Set doc = CreateObject("MSXML.DOMDocument")
doc.Load "C:\Temp\ovfenv.xml"

ns = "xmlns:oe='http://schemas.dmtf.org/ovf/environment/1'"
doc.setProperty "SelectionNamespaces", ns

Set node = doc.selectSingleNode("//PropertySection/Property[@oe:key = 'bootflag']/@oe:value")

If Not node Is Nothing Then
  MsgBox node.nodeValue
Else
  MsgBox "Nothing found!"
End If
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Thank you Tomalak. But unfortunately your solution does not work either :-( Message box keeps reporting nothing found :-(. And what happens when the machine you run this script on has no internet ? ODD that one would have to reference a web site like this not so ? is there no local option ? – user3407537 Mar 12 '14 at 21:00
  • You don't need internet, the namespace is in URL form through convention only. No actual http request is made. Check whether there was a [`parseError`](http://msdn.microsoft.com/en-us/library/ms756041%28v=vs.85%29.aspx) after you load the XML document. If there was no error, test the XPath, bit by bit, until you find where it breaks. Double check everything, because my code actually works against the sample XML you gave. – Tomalak Mar 12 '14 at 21:06
0

Check out fmunkert's answer -> Here

More specifically, the code he documented here:

Set objDoc = CreateObject("MSXML.DOMDocument")
objDoc.Load "C:\Temp\Test.xml"

' Iterate over all elements contained in the <root> element:

Set objRoot = objDoc.documentElement
s = ""
t = ""
For Each child in objRoot.childNodes
   s = s & child.getAttribute("name") & " "
   t = t & child.getAttribute("value") & " "
Next
MsgBox s    ' Displays "alpha beta gamma "
MsgBox t    ' Displays "1 2 3 "

' Find a particular element using XPath:

Set objNode = objDoc.selectSingleNode("/root/property[@name='beta']")
MsgBox objNode.getAttribute("value")     ' Displays 2
Community
  • 1
  • 1
Rich
  • 4,134
  • 3
  • 26
  • 45
  • Thanks for the reply and the tip sadly things don't work when I change the code to suit my XML file and then the node info. I end up getting error 800A01A8 line 9 character 1 : object required 'objRoot' .... I am sure my tag/path is not correct and that is what I have been battling with since the NODE is oe:value and oe:value :-( here is my code based on the info you sent. And I have tried quite a few other examples which work for the usuall tagged xml document but this is really confusing me. Here is my code : – user3407537 Mar 11 '14 at 22:23
  • Set objDoc = CreateObject("MSXML.DOMDocument") objDoc.Load ("c:\Temp\ovfenv.xml") ' Iterate over all elements contained in the element: Set objRoot = objDoc.documentElement s = "" t = "" For Each child in objRoot.childNodes s = s & child.getAttribute("oe:key") & " " t = t & child.getAttribute("oe:value") & " " Next MsgBox s 'show oe:key MsgBox t 'show oe:value ' Find a particular element using XPath: 'Set objNode = objDoc.selectSingleNode("/PropertySection/Property[@oe:key='bootflag']") 'MsgBox objNode.getAttribute("oe:value") 'display value – user3407537 Mar 11 '14 at 22:32
  • And you have edited my whole entire question and the topic ? The code page you refer to on MSDN does not show an example of a vbscript at all ? Please tell me why you have changed my message headline ? You have removed the cricuial line where I seek help and to quote, here is my request :I have been searching all over the web for a vbscript example but am really battling to find one that solves the problem" – user3407537 Mar 12 '14 at 21:12
  • ...Please accept my apology for incorrectly directing ...Still not sure hot to post here :-( and getting so lost in the XML code and confusion :-) – user3407537 Mar 12 '14 at 21:22
  • @Tomalak ..Thanks again for the reply ... Pardon the confusion. Does the ns reference here "xmlns:oe='http://schemas.dmtf.org/ovf/environment/1'" in the code you posted as a ns reference point to the loaded xml document ? Did you try the code you posted and did you have success ? I just keep getting a message box stating nothing found – user3407537 Mar 12 '14 at 21:31
  • @Tomalak..YOU ROCK !! THANK YOU!There error was in the xml file .. hyphens and odd characters! ParseError is awesome it showed failure on loading the .xml. I cleaned up the xml and all worked like a charm! How do you parse both the oe:key and oe:value. Currently the value for single each oe:value node is reported. How do you do it all in one loop/pass without repeating code for each vaule ? Specifically this line Set node = doc.selectSingleNode("//PropertySection/Property[@oe:key = 'bootflag']/@oe:value") MsgBox node.nodeValue. Can you iterate and report all in one pass..THANK YOU ONCE AGAIN! – user3407537 Mar 14 '14 at 11:01