Reputation: 31
I have a string variable containing XML that I would simply like to append to an IXMLDOMNode. Hilariously, this is the most primitive task, yet painfully unintuitive, in MSXML2. (I don't want to use XSLT).
My approach was:
Set xRoot = xDoc2.selectSingleNode("/ns:Root") 'destination parent
Set xNode = NodeFromXML(XMLstring) 'node to append
xRoot.appendChild xNode 'FAIL
----------------------------------------------
Private Function NodeFromXML(XMLstring As String) As IXMLDOMNode
'returns IXMLDOMNode of loaded XML string...
Dim Obj As New MSXML2.DOMDocument60
On Error GoTo handler
With Obj
.async = False
.loadXML XMLstring
Set NodeFromXML = .selectSingleNode("/")
End With
handler:
End Function
The problem with this is that my NodeFromXML function produces a node that is of node type 'DOCUMENT' and thus cannot be appended to an ELEMENT type node.
How should I achieve my goal?
Upvotes: 0
Views: 380
Reputation: 167506
In the W3C DOM there are methods importNode
(level 2) and adoptNode
(level 3) that help to be able to insert a node owned by one document into another document.
I think MSXML doesn't usually care whether DOM nodes are owned by different documents, unless it is MSXML 6 and different threading models, where you then need the there available importNode
method.
But for your sample, in the DOM, what you can insert into an element node like you selected as xRoot
, with appendChild
, is appropriate nodes e.g. element nodes, text nodes, comment nodes, processing instruction nodes. For your sample that means just return the documentElement
so e.g. Set NodeFromXML = Obj.documentElement
.
Then I would expect the appendChild
to work.
If not, try e.g. appendChild(xDoc2.importNode(xNode, True))
.
Upvotes: 1