分析 XML - 如何循环访问子节点并为每个子节点赋值



我正在尝试解析以下XML:

  <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并且我超出了我的头。

到目前为止,我有这个 -

 Set objHosts = objConfigXml.selectNodes("//host/object")
     For Each objMap in objHosts
     Set objSingle = objMap
        objSheet.Cells(iY,1).Value = objSingle.selectSingleNode("objectname").Text
        objSheet.Cells(iY,2).Value = objSingle.selectSingleNode("objecttype").Text
        objSheet.Cells(iY,3).Value = NEED HELP
        objSheet.Cells(iY,4).Value = NEED HELP
        objSheet.Cells(iY,5).Value = objSingle.selectSingleNode("location").Text

接下来,我必须检索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

输出 #1:

End tag 'object' does not match the start tag 'fcadapterports'.

输出 #2(关闭标签后):

<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

应显示

  1. 错误检查的方式和原因,以捕获错误,例如丢失</fcadapterports>
  2. 通过 index、firstChild 和 For Each 访问子节点
  3. 在循环访问(子节点)集合时使用额外信息 - 例如列号

更新评论:

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

有 2 个问题的作品:

  1. 它将所有值放在单个 excel 单元格中(因为objSheet.Cells(iY,4) = ...每次都把所有东西都放进去objSheet.Cells(iY,4))

  2. 它不是遍历 - 它只选择最后一个值(因为步进计数循环是无稽之谈(与对于我提议的每个))

我仍然想写一个免责声明:

这个答案是程序员使用的,而不是复制和粘贴艺术家 谁无视所有建议。

OTOH,也许改变路线

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

将有助于理解我所说的"在循环访问(子节点)集合时使用额外信息 - 例如列号 - "的含义

最新更新