Bhat wrote:

Bhat wrote:
Hi,

I have written a small function that dumps portions of the DOM tree
(rooted at nodes with the specified tag name) to a file [see below]. Obviously the output file does not have a root node. How can I make the
output file a proper XML file (with a single root node)?  I have tried a
few things, but nothing seems to work.  My function follows:



Here's what I did:
First I created a new DOM document using the
DOMImplementation::createDocument() method.  For this, I had to provide the
name of the root node.  I traversed the original XML file and when I found
the DOMNodes with the desired tag name (in the original XML file), I did a
deep cloning of the nodes [using DOMNode::cloneNode(true)].  Then I tried
the following options (all unsuccessful):

1).  I appended the cloned nodes as a child to the new DOM document, using
the appendChild() method.  This caused an exception to be thrown (sadly
cannot decipher the exception yet)
This won't work, because the nodes don't belong to the document. Also, a document can only have a single element child.


2). I obtained the root node of the new XML document , using the
getDocumentElement() method, and tried to append the cloned nodes to the
root node.  Again, this caused an exception to be thrown (sadly cannot
decipher the exception yet)
This won't work, because the nodes don't belong to the document.


3). I imported the cloned nodes into the new DOM document, using the
DOMDocument::importNode(DOMNode*, true) call.  This time there was no
exception, but the subtrees rooted at the cloned nodes dif not get attached
at all.
Yes, because importNode doesn't "attach" the nodes. You need to append the nodes to the document element of the new document.

What you are doing is incredibly inefficient for the purposes of just generating a single root node.

Dave

Reply via email to