Discussion:
Insert contents in one XML to another
(too old to reply)
Alamelu
2006-11-11 12:25:01 UTC
Permalink
I want to insert the contents in one XML to another.

Sample1.xml
<?xml version="1.0"?>
<Root xmlns:dt="urn:schemas-microsoft-com:datatypes" Version="1">
<VERSION dt:dt="string">Default</VERSION>
<NO dt:dt="string">Default</NO>
<SERIA dt:dt="string">Default</SERIAL>
<TIME dt:dt="dateTime">2006-10-30T16:57:32</TIME>
</Root>

I took the rootelement of Sample1.xml and tried to insert the contents to
another xml
I used AppendChild method

hRes = pNodeinSample2XML->raw_appendChild(pRootElementofSample1xml,
&pTempNode);

The problem with this approach was, it was including the root tag also...
(i.e) it was including
<Root xmlns:dt="urn:schemas-microsoft-com:datatypes" Version="1">

I want to insert the contents in sample1.xml without this Root Tag

I thought of getting the rootelement of sample1.xml, then loop through all
child nodes and copy each node to the target.

Are they any simple approach to do this without copying the Root Tag?

Regards,
Alamleu
Martin Honnen
2006-11-11 15:03:55 UTC
Permalink
Post by Alamelu
I want to insert the contents in sample1.xml without this Root Tag
I thought of getting the rootelement of sample1.xml, then loop through all
child nodes and copy each node to the target.
Are they any simple approach to do this without copying the Root Tag?
No, do e.g.
var root = xml1.documentElement;
while (root.hasChildNodes()) {
node.appendChild(root.firstChild);
}
That is a safe way while a normal loop over childNodes will not work as
the childNodes.length changes during the loop.

Also note that later versions of MSXML (MSXML 5 and later I think) do
not allow moving a node from one document to another, instead you have
to use importNode e.g.
for (var root = xml1.documentElement, childNodes = root.childNodes, l
= childNodes.length, i = 0; i < l; i++) {
node.appendChild(node.ownerDocument.importNode(childNodes[i], true));
}
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Loading...