adding existing JDOM elements
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Brett_Astleford
Posted On:   Wednesday, March 12, 2003 08:30 AM

Hi all, I am having trouble adding existing elements to a document object. Basically, I am trying add a new element to an existing xml document. I am using SAX and JDOM. Is this possible? I have not found many tutorials on modifying existing xml data. System.out.println("LAME"); File f = new File("C:\TEMP\blue_card.xml"); System.out.println("LAME"); Document doc = x.createDoc(f); //A functions that creates a SAX doc System.out.println("LAME"); Element roo = doc.getRootElement(); List lister = roo.getChildren(); ListIterator li = lister.listIterator(); while(li.hasNext()){ Ele   More>>

Hi all,

I am having trouble adding existing elements to a document object.
Basically, I am trying add a new element to an existing xml document.
I am using SAX and JDOM.
Is this possible?
I have not found many tutorials on modifying existing xml data.




			
System.out.println("LAME");
File f = new File("C:\TEMP\blue_card.xml");
System.out.println("LAME");
Document doc = x.createDoc(f); //A functions that creates a SAX doc
System.out.println("LAME");
Element roo = doc.getRootElement();
List lister = roo.getChildren();
ListIterator li = lister.listIterator();
while(li.hasNext()){
Element elm = (Element)li.next();
root.addContent(elm); //adding existing XML elements/children to new root element
}
root.addContent(paddy); //new element/data


Then I write to file... Error says parent element already exists.
Can I not add existing elements to a new root element/document?
Any help appreciated. Thanks!!!

   <<Less

Re: adding existing JDOM elements

Posted By:   Roseanne_Zhang  
Posted On:   Wednesday, March 12, 2003 02:58 PM

In JDOM, use (org.jdom.Element)org.jdom.Element.clone() will solve your problems. JDOM clone is a deep clone and it will forget its parent etc.



In DOM, you need to use

org.w3c.dom.Document.importNode(node, true)

to make the node forget its parent and switch the container Document.
About | Sitemap | Contact