Combine two XMLnodelist in VBA

雨燕双飞 提交于 2019-12-24 11:47:50

问题


I have to XMLnodelists which is want to combine to one. So I would like to copy the content of oNodelist_B into oNodeList_A

Set oNodeList_A = xmldoc.getElementsByTagName("A")
Set oNodeList_B = xmldoc.getElementsByTagName("B")

anyone have an idea.?


回答1:


This works for me:

Sub Tester()
   Dim xmlDoc As New MSXML2.DOMDocument30
   Dim objNodes As IXMLDOMNodeList, o As IXMLDOMElement

  xmlDoc.async = False

  'xmlDoc.Load "D:\Analysis\config.xml"
  xmlDoc.LoadXML Range("A1").Value

  xmlDoc.setProperty "SelectionLanguage", "XPath" '<< required!

  If xmlDoc.parseError.errorCode <> 0 Then

    MsgBox "Error!" & vbCrLf & _
    "  Line: " & xmlDoc.parseError.Line & vbCrLf & _
    "  Text:" & xmlDoc.parseError.srcText & vbCrLf & _
    "  Reason: " & xmlDoc.parseError.reason

  Else

    Set objNodes = xmlDoc.SelectNodes("root/A|root/B")
    If objNodes.Length = 0 Then
      Debug.Print "not found"
    Else
      For Each o In objNodes
        Debug.Print o.tagName, o.nodeTypedValue
      Next o
    End If

  End If

End Sub

Test XML:

<?xml version="1.0"?>
<root>
     <A>ValA1</A>
     <A>ValA2</A>
     <B>ValB1</B>
     <B>ValB2</B>
</root>


来源:https://stackoverflow.com/questions/34364326/combine-two-xmlnodelist-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!