Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

B*tch of a MSXML!

Status
Not open for further replies.

SiJP

Programmer
May 8, 2002
708
GB
I have the following xml in a DOM object:

<root>
<data>
<foo>blarg</foo>
</data>
</root>

I also have this xml in another DOM object.

<somethingelse>
<foo>blarg</foo>
</somethingelse>

The challenge is to:

1) Create a new node after the <data> node called <moredata>
2) Insert the xml from the second dom object, into this node..

So the final output should be:

<root>
<data>
<foo>blarg</foo>
<moredata>
<somethingelse>
<foo>blarg</foo>
</somethingelse>
</moredata>
</data>
</root>

I have tried along the lines of:

set objNodeNew = objXML.createElement("moredata")
Set objNode = objXML.selectSingleNode("//data")
objNode.parentNode.appendChild objNodeNew

but.. I'm coding rubbish..!

Any help would be great :)

------------------------
Hit any User to continue
 
Ok.. need to calm down before I post in the future.
Sorry for any offence the topic caused.

I have figured this out by:

1) Set objNodeNew = objXML.createNode(NODE_ELEMENT, "moredata", "")
2) objNodeNew.Text = DomObj2.XML
3) Set objNode = objXML.selectSingleNode("//data")
4) objNode.parentNode.appendChild objNodeNew

:)

------------------------
Hit any User to continue
 
Because the two document fragments have different DomDocument parents, you can't just copy the object references over (as you've discovered). You have to use the appendChild method, which resets the parent property of the nodes.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks Chip. I'm actually finding that 2) objNodeNew.Text = DomObj2.XML doesn't do what I thought it did. Instead, I get '&gt's all over the shop.

Have to find another way round this now!

------------------------
Hit any User to continue
 
Ok, this is how I finally did it:

Code:
Dim domObj1 As MSXML2.DOMDocument
Dim domObj2 As MSXML2.DOMDocument
Dim docFrag As IXMLDOMDocumentFragment
Dim domNode As IXMLDOMNode

...

'Create a document fragment for the temp object
Set docFrag = domObj1.createDocumentFragment
docFrag.appendChild domObj2.firstChild.cloneNode(True)
                    
'Append XML to the relevant node
Set domNode = domObj1.selectSingleNode("//data")
domNode.parentNode.appendChild docFrag


Si

------------------------
Hit any User to continue
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top