File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Understanding adding to XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Understanding adding to XML" Watch "Understanding adding to XML" New topic
Author

Understanding adding to XML

Tom Sullivan
Ranch Hand

Joined: Dec 20, 2005
Posts: 72
I have never had need to add XML elements in files before other than the initial creation of them with a printwriter object to write the XML to disk. Now I have a project that requires that I parse existing XML and find certain elements and to add child elements and values to those children. I can parse the document and add the child element through:



Once I have this value in my DOM, is there any open source API or package in Java that you are aware of that will take the modified DOM and just rewrite the xml file on disk with it? Or, should I just create my own methods to do the same thing (overwriting the file with some precautions taken)?
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Do you mean like this?

You already have revised the xml inside the DOM object, and you want just to save the xml under it?

If so, try this:

The DOM implementation you used to revise the xml, can do it for you too.


private static final String MY_FILE = "revised.xml";

....do the xml revision here


PrintWriter writer = new PrintWriter(new FileWriter(MY_FILE));
writer.println(((DocumentImpl)doc).saveXML(null));
writer.close();


The 'doc' variable is your variable that 'is a' DocumentImpl.
Tom Sullivan
Ranch Hand

Joined: Dec 20, 2005
Posts: 72
That will do take care of it I am sure. Thanks for the help and guidance.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
I think my code is for xerces implementation of dom. Other implementations might have their own 'method' too.

What are you using? xerces? jaxp?
Tom Sullivan
Ranch Hand

Joined: Dec 20, 2005
Posts: 72
After I read your post, I did a search on "documentimpl" and xerces first popped up so I figured that was what you were referencing. I have been just using org.xml.sax and org.w3c.dom packages of the Java API. I was just about to read about xerces (which I had heard of but never had need to study before). From your suggestion, it sounds like that package will do what I need.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42359
    
  64
DOM version 3 introduced a standard way to load and save DOM objects. If you are running Java 5, you have DOM 3 built in, and should use that instead of parser-specific classes (which you would need to change if you ever want to use a different parser). Look for the org.w3c.dom.ls package in your JDK javadocs.


Ping & DNS - my free Android networking tools app
Tom Sullivan
Ranch Hand

Joined: Dec 20, 2005
Posts: 72
LSSerializer and DOMImplementaionLS looks like it may work... I figured that there was something in the API that would take care of this. Thanks very much. That saves me having to learn another API right now!
 
Consider Paul's rocket mass heater.
 
subject: Understanding adding to XML