解析XML - 如何遍历子节点并为每个节点赋值

时间:2014-04-02 20:06:22

标签: vbscript xmldom

我正在尝试解析以下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>

所以我可以获得价值,直到我到达了我的头脑,而且我已经超越了我的头脑。

到目前为止,我有这个 -

 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)等。

1 个答案:

答案 0 :(得分:0)

这个演示脚本:

  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. 等错误
  3. 通过index,firstChild和For Each
  4. 访问c​​hildNodes
  5. 使用额外信息 - 例如列号 - 循环遍历(childNodes)集合时
  6. 更新wrt评论:

    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
    

    有两个问题:

    1. 它将所有值放在一个excel单元格中(因为 objSheet.Cells(iY,4)= ...每次放入所有内容 objSheet.Cells(1Y,4))

    2. 它没有迭代 - 它只选择最后一个值(因为 步数计数循环是无意义的(恰好相反 我建议的For Each))

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

        

      这个答案将由程序员使用,而不是由复制和粘贴艺术家使用   谁不理会所有建议。

      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
      

      将有助于理解我的意思&#34;使用额外信息 - 例如列号 - 循环遍历(childNodes)集合&#34;