本文介绍了在VBA中合并两个XMLnodelist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将XMLnodelists合并为一个.所以我想将oNodelist_B的内容复制到oNodeList_A

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")

有人有主意吗??

推荐答案

这对我有用:

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

测试XML:

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

这篇关于在VBA中合并两个XMLnodelist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 03:37