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 how to modify XML? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "how to modify XML?" Watch "how to modify XML?" New topic
Author

how to modify XML?

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Feb 11, 2002
Posts: 17
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

Joined: Mar 22, 2000
Posts: 12803
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to modify XML?