Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to modify XML?

 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
lets say I have a XML file,

I want to modify "Mit" and put "Vasavada" there via Java XML api. I know how to use Java XML API and all but my question is-
1. We have to read the file in DOM object and then modify the element for "Mit" and write the "whole" file back right?
How can we avoid writing whole file back? Is there a way? This is because if this XML file too big then it would be costly operation to modify a single entry like this, right?
Regards
Maulin
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With standard DOM, the only way is to write the whole document back to disk.
There probably are some APIs that allow one to modify the document in a "random access file" way, but I'm unable to point you to one right now.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we avoid writing whole file back? Is there a way? This is because if this XML file too big then it would be costly operation to modify a single entry like this, right?

Depends on what your application needs to do....if its only for "rendering", you could use a style sheet and attache the style sheet to your XML file. That way you can load the xml file and style sheet nd render the data. If you want to "persist" the change, then you need to write to the disk (or a DB).
regds.
- m
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
thank you for your answers.
madhav,
I want persistent storage in xml so...I guess I will end up writing whole DOM back..
Regards
Maulin
 
Yu Chen
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about I just want to filter the source XML based on some rules, and then I just need to write the new XML to another Result without changes to the source XML. In this case, what is the proper API I should use for some modifications like, for example, adding a new attribute to a node.

Thanks all,
==
yc
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should look into SAX processing - it makes large documents much easier as long as the tranformation required is simple. You would essentially be reading/parsing the original XML document and writing the revised version at the same time.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic