2017-08-10 87 views
0

我读像一个XML文件中:阅读两个XML文件和第二个XML追加到第一个XML节点

Set xml1 = CreateObject("Msxml2.DOMDocument") 
Set xml2 = CreateObject("Msxml2.DOMDocument") 
xml2.Async = "False" 
xml1.Async = "False" 

xml1.Load("file1.xml") 
xml2.Load("file2.xml") 

Set xml1ChildNode = xml1.selectNodes("/node") 

xml1ChildNode.AppendChild(xml2) 

虽然我不断收到:

Error: Object doesn't support this property or method: 'xml1ChildNode.appendChild'

这是为什么?

回答

1
  • 得到你想要从文件1.

  • 追加获取节点相要从文件2追加的节点列表。

  • 循环从文件1获取的节点列表并追加。

调查评论的详细信息。

Set xml1 = CreateObject("Msxml2.DOMDocument") 
Set xml2 = CreateObject("Msxml2.DOMDocument") 

xml1.Async = "False" 
xml2.Async = "False" 

xml1.load("C:\\Users\\XXXXX\\Desktop\\test\\file1.xml") 
xml2.load("C:\\Users\\XXXXX\\Desktop\\test\\file2.xml") 

'GET THE NODES TO APPEND FROM XML 1 ex:book 
Set objNodeList = xml1.getElementsByTagName("book") 

'GET THE NODES TO WHICH YOU WANT TO APPEND IN XML 2 ex:catalog 
Set ObjectRecord = xml2.getElementsByTagName("catalog") 

'LOOP THE NODES AND APPEND 
For Each objNode in objNodeList 
    'APPEND TO WHATEVER ELEMENT YOU WANT in xml 2 ex: first catalog element in XML 2 
    ObjectRecord(0).appendChild objNode 
Next 

'CHECK YOUR OUTPUT 
xml2.Save "C:\\Users\\XXXXX\\Desktop\\test\\file3.xml" 
0

一对夫妇的更新

  • 的selectNodes返回节点的集合,所以你必须来遍历它。使用selectSingleNode来获得一个。

  • appendChild从父类(xml2.DocumentElement或类似的)开始,以及你想要附加到它的内容。

  • 这实际上将节点从一个文档移动到另一个文档。如果您不想这样做,请在appendChild中使用 cloneNode。

下面是更新后的代码

Set xml1 = CreateObject("Msxml2.DOMDocument") 
Set xml2 = CreateObject("Msxml2.DOMDocument") 
xml2.Async = "False" 
xml1.Async = "False" 

xml1.Load("file1.xml") 
xml2.Load("file2.xml") 

For Each ndNode In xml1.selectNodes("//node") 
    xml2.DocumentElement.appendChild(ndNode) 
Next 

WScript.Echo xml2.xml