【问题标题】:Append a node to a node in XML将节点附加到 XML 中的节点
【发布时间】:2013-12-04 00:48:40
【问题描述】:

我正在尝试了解 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

【问题讨论】:

  • 欢迎来到 SO!你能指出收到错误的确切代码行吗?
  • 我收到以下错误:xNode2.appendChild (xElement) 我哪里出错了。
  • @user2962310 你有解决这个问题的办法吗?我面临着几乎同样的问题..

标签: xml vb6 msxml appendchild


【解决方案1】:

您需要将节点从原来的 DOM 中import 到新的 DOM 中,然后才能将该节点添加到新的 DOM 中。

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

【讨论】:

    猜你喜欢
    • 2014-12-15
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多