感谢一个非常棒的论坛,它真的是丰富的知识,希望能够找到一个可以帮助别人的解决方案。
我想知道某个善良的灵魂是否可以帮助我建议如何或可能使用代码示例指向我正确的方向来解析此处显示的xml代码。 我一直在网上搜索一个vbscript示例,但我真的很想找到一个解决问题的方法。
网络上的大多数示例都展示了如何在节点或子节点中提取某些标签或文本匹配,并且在本代码中引用我感兴趣的部分并不是很好解释。
我已经有python代码工作正常但是vbscript让我难过: - (
xmlDOC定义了vAPP的环境设置,我感兴趣的部分是:oe:key
和oe:value
<PropertySection>
下的每个节点。
我需要将密钥和值存储在包含
等条目的文本文件中bootflag=a clusetername=cluster test
等。这是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>
我真的很感激任何帮助。
编辑:根据下面的回复,我尝试了这段代码,但我确定填写节点信息时出错,特别是#34; oe:key&#34;和&#34; oe:价值&#34;。
请在下面找到更新代码:
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")
答案 0 :(得分:2)
你几乎拥有它。
您必须 - 在MSXML中就像在其他所有API实现中一样 - 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
答案 1 :(得分:0)
查看fmunkert的回答 - &gt; 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