Vbscript 解析XML—如何迭代子节点并为每个子节点分配值
我正在尝试解析以下XML:Vbscript 解析XML—如何迭代子节点并为每个子节点分配值,vbscript,xmldom,Vbscript,Xmldom,我正在尝试解析以下XML: <host> <object> <objectname>Server1</objectname> <objecttype>host</objecttype> <location>USA</location> <fcadapterports> <port&g
<host>
<object>
<objectname>Server1</objectname>
<objecttype>host</objecttype>
<location>USA</location>
<fcadapterports>
<port>
<portwwn>1000-0000-CCXX-B2B4</portwwn>
</port>
<port>
<portwwn>1000-0000-AW8D-23AB</portwwn>
</port>
</object>
</host>
接下来,我必须检索portwwn的多个值,并将这些值分配给电子表格中的下一个单元格,如objSheet.cells(iY,3)和objSheet.cells(iY,4)等。此演示脚本:
Dim sXml : sXml = Join(Array( _
"<host>" _
, " <object>" _
, " <objectname>Server1</objectname>" _
, " <objecttype>host</objecttype>" _
, " <location>USA</location>" _
, " <fcadapterports>" _
, " <port>" _
, " <portwwn>1000-0000-CCXX-B2B4</portwwn>" _
, " </port>" _
, " <port>" _
, " <portwwn>1000-0000-AW8D-23AB</portwwn>" _
, " </port>" _
, " </fcadapterports>" _
, " </object>" _
, "</host>" _
))
Dim sXPath : sXPath = "/host/object/fcadapterports"
Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument")
objMSXML.setProperty "SelectionLanguage", "XPath"
objMSXML.async = False
objMSXML.loadXml sXml
If 0 = objMSXML.parseError Then
Dim ndFnd : Set ndFnd = objMSXML.SelectSingleNode(sXPath)
If ndFnd Is Nothing Then
WScript.Echo qq(sXPath), "not found"
Else
WScript.Echo ndFnd.xml
WScript.Echo ndFnd.childNodes(0).tagName, "1", ndFnd.childNodes(0).firstChild.text
WScript.Echo ndFnd.childNodes(1).tagName, "2", ndFnd.childNodes(1).firstChild.text
Dim nCol : nCol = 3
Dim ndChild
For Each ndChild In ndFnd.childNodes
WScript.Echo "col", nCol, ndChild.SelectSingleNode("portwwn").text
nCol = nCol + 1
Next
End If
Else
WScript.Echo objMSXML.parseError.reason
End If
输出#2(关闭标记后):
有两个问题的作品类型:
WScript.Echo "col", nCol, ndChild.SelectSingleNode("portwwn").text
到
新产量
cscript 22821889.vbs
..
1000-0000-CCXX-B2B4 should go in objSheet.Cells(iY,3).Value
1000-0000-AW8D-23AB should go in objSheet.Cells(iY,4).Value
将有助于理解“在(childNodes)集合上循环时使用额外信息(例如列号)”的含义。我需要取两个值:1000-0000-CCXX-B2B4 1000-0000-AW8D-23AB,并将这两个值插入以下行:objSheet.Cells(iY,3)。Value=????和objSheet.Cells(iY,4).Value=?????抱歉,这一切都不起作用。我正在尝试将此代码与我已有的代码相匹配。我已经有一个set obj.ConfigXml=CreateObject(“Microsoft.XMLDOM”)语句。我已经尝试了我能想到的你的代码的每一种变体,以使它在现有代码中工作,但它不是。它将所有的值放在一个excel单元格中,并将其设置为2。它没有遍历-它只选取最后一个值:Set nodeslist=objConfigXml.selectNodes(“//host/object/fcadapterports/port”),i=0到nodeslist.Length-1步骤2 objSheet.Cells(iY,4)。value=nodeslist(i)。Text&nodeslist(i+1)。Text Next
<fcadapterports>
<port>
<portwwn>1000-0000-CCXX-B2B4</portwwn>
</port>
<port>
<portwwn>1000-0000-AW8D-23AB</portwwn>
</port>
</fcadapterports>
port 1 1000-0000-CCXX-B2B4
port 2 1000-0000-AW8D-23AB
col 3 1000-0000-CCXX-B2B4
col 4 1000-0000-AW8D-23AB
Set nodeslist = objConfigXml.selectNodes("//host/object/fcadapterports/port")
For i = 0 To nodeslist.Length -1 Step 2
objSheet.Cells(iY,4).Value = nodeslist(i).Text & nodeslist(i+1).text
Next
WScript.Echo "col", nCol, ndChild.SelectSingleNode("portwwn").text
WScript.Echo ndChild.SelectSingleNode("portwwn").text, "should go in objSheet.Cells(iY," & nCol & ").Value"
cscript 22821889.vbs
..
1000-0000-CCXX-B2B4 should go in objSheet.Cells(iY,3).Value
1000-0000-AW8D-23AB should go in objSheet.Cells(iY,4).Value