问题
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