Combine two XMLnodelist in VBA

2019-08-10 05:47发布

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条回答
Root(大扎)
2楼-- · 2019-08-10 06:27

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>
查看更多
登录 后发表回答