Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

memory efficient way to modify xml in java  RSS feed

 
Santanu Guha
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to modify a single information in XML file . XML file is about 100 lines . For modifying a single element in whole XML file what would be the most memory efficient way in JAVA ?

JAXB is better ?
Simple SAX parser ?

I have to change that single node information in an XML file . But the count of those XML file can be huge around 5000 to 10000 in for loop

or any other way .....Kindly suggest .....
 
fred rosenberger
lowercase baba
Bartender
Posts: 12441
42
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my first thought would be to not use java at all, but something like perl or awk...or if it's a one-time thing, even a simple editor should be able to to a quick search-and-replace.

But there may be other conditions you've not told us, so those may not work.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option might be to use XSL. There's a Java component called xalan that can be run as a stand-alone application or invoked from Java code. I use it extensively for XML transformations.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your XML file isn't especially large, so I don't understand why you're handicapping your search by requiring the most memory-efficient way to do these transformations.

I'd agree with Tim and use XSLT to do the transformation on each of those XML documents, if that's suitable for the change you need to do. You can save time by using a Templates object once and reusing it, rather than creating a new Transformer object for each document... although you didn't require a speed-efficient method, so maybe that doesn't matter to you.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The lowest memory use would also be quite fast.

In any case you need to create a new file with a temporary name to contain the modified content.

Parse using SAX or SaxT, writing the content of each event to the new file until you hit the element to get new content. Write the new content then continue parsing and writing till you hit the end of the document.

Delete the original file and rename the temp new file.

Bill
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!