• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding adding to XML

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)?
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That will do take care of it I am sure. Thanks for the help and guidance.
 
Jesus Angeles
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tom Sullivan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic