Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)

I need to read smallish (few MB at the most, UTF-8 encoded) XML files, rummage around looking at various elements and attributes, perhaps modify a few and write the XML back out again to disk (preferably with nice, indented formatting).

What would be the best XML parser for my needs? There are lots to choose from. Some I'm aware of are:

  • JDOM
  • Woodstox
  • XOM
  • dom4j
  • VTD-XML
  • Xerces-J
  • Crimson

And of course the one in the JDK (I'm using Java 6). I'm familiar with Xerces but find it clunky.

Recommendations?

2 Answers

0 votes
by (46k points)

If rate and memory is no obstacle, dom4j is a very helpful option. If you need rate, using a StAX parser like Woodstox is the correct way, but you need to write more code to get something done and you have to get applied to process XML in streams.

0 votes
by (108k points)

I think you should not acknowledge any specific parser implementation. Java API for XML Processing lets you use any agreeing parser implementation in a standard way. The code should be much more portable, and when you realize that a specific parser has grown too old, you can replace it with another without changing a line of your code (if you do it correctly).

There are three ways of managing XML in a standard way:

  • SAX: This is the simplest API. You read the XML by defining a Handler class that receives the data inside elements/attributes when the XML gets processed serially. It is quicker and simpler if you only plan to read some attributes/elements and/or write some values back (your case).
  • DOM: This method generates an object tree that lets you modify/access it randomly so it is better for complex XML manipulation and handling.
  • StAX: This is in the midst of the path between SAX and DOM. You just have to write code to pull the data from the parser you are interested in when it is processed.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 13, 2019 in Java by Ritik (3.5k points)
0 votes
1 answer
asked Sep 29, 2019 in Java by Shubham (3.9k points)
0 votes
1 answer
asked Nov 12, 2019 in Java by Anvi (10.2k points)

Browse Categories

...