You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Gervase Markham <GR...@dataconnection.com> on 2000/09/12 18:19:45 UTC

RE: Creating copies of an XML document

> I have been fiddling with Xercces 1.1.3 lately and was 
> wondering whether it was
> possible to create a copy of an entire document in memory 
> while maintaining all
> the original document's attributes.  What I am trying to 
> accomplish is to read
> in an empty XML file on program (for use as a 'template') and 
> then obtain
> copies of that document in individual threads which then go 
> on to fill in the
> information.  
> Is what I am trying to do possible, and if so, how would one 
> go about coding it
> (i.e. which methods should be used)?

I believe the Document method:
public Node importNode(Node importedNode,
                       boolean deep)
                throws DOMException
may be what you are looking for :-) You'd set "deep" to true.

Gerv

Re: Creating copies of an XML document

Posted by Arnaud Le Hors <le...@us.ibm.com>.
Importing the nodes into the new document doesn't put them into the
document tree. You're missing a call to insert the new root into the
document. Try the following:

  Node newRoot = newDoc.importNode(doc.getDocumentElement(), true);  
  newDoc.appendChild(newRoot);
-- 
Arnaud  Le Hors - IBM Cupertino, XML Technology Group

RE: Creating copies of an XML document

Posted by Mayer Crystal <mc...@pro-found.com>.
Gerv,

  I thought that might be it, but I've encountered other problems using that
method.  In particular, the code that I've used looks like:

...
Document originalDoc = parser.getDocument();
Document newDoc = null;

try  {
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  newDoc = db.newDocument();
  newDoc.importNode(doc.getDocumentElement(), true);  
 }
catch (Exception e)  {
  e.printStackTrace();
}
 ...
NodeList nl = newDoc.getElementsByTagName("PurchaseOrder"); 	//This exists
in the original
...

When this is executed, the last line above returns nothing even though the
element exists in the original document.  Any help would be appreciated.

Thanks,
Mayer


> I believe the Document method:
> public Node importNode(Node importedNode,
>                        boolean deep)
>                 throws DOMException
> may be what you are looking for :-) You'd set "deep" to true.
> 
> Gerv
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org