将节点附加到XML中的节点

时间:2013-11-06 21:05:47

标签: xml vb6 msxml appendchild

我正在尝试了解MSXML及其用法。我有一个项目,我需要从xml文件中读取节点及其所有内容,然后将该节点复制到另一个xml文件。我坚持追加子节点。我认为我正在做正确的事情,但我不断收到错误“对象不支持此属性或方法”。

它挂起“xNode2.appendChild(xElement)”

我已附上我正在处理的程序。

如果有人能给我一些指示,我将不胜感激。

Dim xDoc As DOMDocument60, xDoc2 As DOMDocument60, xNode As IXMLDOMElement, _
    xmlStr As String, xSub As IXMLDOMNode, xNode2 As IXMLDOMElement

Set xDoc = New DOMDocument60

'Attempt to load the backup file for the selected printer.
If xDoc.Load("C:\Program Files\ID Technology\CiControl\Backup Files\" & _
              main.printerView.SelectedItem.Key & "\" & _
              Format(Date, "mm.dd.yy") & ".xml") Then

    'Find the message xml in the backup file.
    Set xNode = xDoc.selectSingleNode("//Messages/Message[Name='" & msgView.SelectedItem & "']")

    'Begin extracting all of the message xml.
    For Each xSub In xNode.childNodes            
        Debug.Print xSub.xml
        'Build the xml string.
        xmlStr = xmlStr & xSub.xml
    Next

    Set xDoc2 = New DOMDocument60

    xDoc2.Load ("C:\Program Files\ID Technology\CiControl\Backup Files\127.0.0.1\" _
                & Format(Date, "mm.dd.yy") & ".xml")

    'Set xNode2 = xDoc2.createElement("Message")
    Set xNode2 = xDoc2.selectSingleNode("//Messages")

    Dim xElement As IXMLDOMElement

    Set xElement = xDoc2.createElement("Message")

    xElement.Text = xmlStr

    xNode2.appendChild (xElement)
    'xDoc2.documentElement.appendChild (xNode2)

    xDoc2.save ("C:\Program Files\ID Technology\CiControl\Backup Files\127.0.0.1\" & _
                Format(Date, "mm.dd.yy") & ".xml")

End If

Set xDoc = Nothing
Set xNode = Nothing
Set xSub = Nothing
Set xDoc2 = Nothing
Set xNode2 = Nothing

1 个答案:

答案 0 :(得分:0)

在将节点添加到新DOM之前,您需要import从原始DOM到新DOM的节点。

Dim CloneNode As IXMLDOMNode
Set CloneNode = xDoc2.ImportNode(xElement)
xNode2.appendChild (CloneNode)