Santanu Guha
Santanu Guha

Reputation: 101

Memory efficient way to modify XML in Java

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 ?

  1. JAXB is better ?
  2. Simple SAX parser ?

or any other way .....Kindly suggest .....

Upvotes: 2

Views: 1610

Answers (2)

Michael Kay
Michael Kay

Reputation: 163342

Your files are not big. The memory used to hold a 100-line XML file costs about as much as 5 milliseconds of a programmer's time. I would question your requirement: why do you need to do it in "the most memory efficient way"? I would use XSLT or JDOM2, unless there is clear quantified information that this will not meet externally-imposed performance requirement, which cannot be solved by buying a bit more memory.

Upvotes: 1

prash
prash

Reputation: 1016

SAX parser gives more control on parsing and is faster than DOM parser. JAXB will be easy from the sense of less code writing. XStream is also another option but that is similar to JAXB which is a high level API, so it has some overhead task so it will be bit slower then SAX. I will not suggest for direct string manipulation (applying String.indexOf() and String.replace()) although would be fastest way for updating a unique tag in the XML but its risky as your XML might not be valid and if xml structure is not simple then there will be risk of updating wrong level tag :-)

Therefore, SAX parser looks the best bet to me.

Upvotes: 1

Related Questions