Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easier way to parse XML in Java?

Tags:

java

android

xml

I'm trying to figure out how to parse some XML (for an Android app), and it seems pretty ridiculous how difficult it is to do in Java. It seems like it requires creating an XML handler which has various callbacks (startElement, endElement, and so on), and you have to then take care of changing all this data into objects. Something like this tutorial.

All I really need is to change an XML document into a multidimensional array, and even better would be to have some sort of Hpricot processor. Is there any way to do this, or do I really have to write all the extra code in the example above?

like image 678
Kyle Slattery Avatar asked Nov 12 '09 01:11

Kyle Slattery


People also ask

Does Java have built in XML parser?

Java provides multiple options to parse XML documents. Following are the various types of parsers which are commonly used to parse XML documents. Dom Parser − Parses an XML document by loading the complete contents of the document and creating its complete hierarchical tree in memory.

Which XML parser is faster?

DOM Parser is faster than SAX Parser. Best for the larger sizes of files. Best for the smaller size of files. It is suitable for making XML files in Java.

How many ways we can parse XML file?

Java provides many ways to parse an XML file. There are two parsers in Java which parses an XML file: Java DOM Parser. Java SAX Parser.

What are the two methods of parsing in XML document?

To read and update, create and manipulate an XML document, you will need an XML parser. In PHP there are two major types of XML parsers: Tree-Based Parsers. Event-Based Parsers.


1 Answers

There are two different types of processors for XML in Java (3 actually, but one is weird). What you have is a SAX parser and what you want is a DOM parser. Take a look at http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/ for how to use the DOM parser. DOM will create a tree which you can navigate pretty easily. SAX is best for large documents but DOM is much easier if slower and much more memory intensive.

like image 123
stimms Avatar answered Sep 24 '22 13:09

stimms