I have a somewhat large file (~500KiB) with a lot of small elements (~3000). I want to pick one element out of this and parse it to a java class.
Attributes Simplified
<xml>
<attributes>
<attribute>
<id>4</id>
<name>Test</id>
</attribute>
<attribute>
<id>5</id>
<name>Test2</name>
</attribute>
&开发者_JS百科lt;!--3000 more go here-->
</attributes>
class Simplified
public class Attribute{
private int id;
private String name;
//Mutators and accessors
}
I kinda like XPath, but people suggested Stax and even VDT-XML. What should I do.
500 kb is not that large. If you like XPath, go for it.
I kinda like XPath, but people suggested Stax and even VDT-XML. What should I do.
DOM, SAX and VTD-XML are all three different ways to parse a XML document. Roughly in this order of memory efficiency. DOM needs over 5 times of memory as XML file big is. SAX is only a bit more efficient, VTD-XML uses only a little more memory than the XML file big is, about 1.2 times.
XPath is just a way to select elements and/or data from a (parsed) XML document.
With other words, you can just use XPath in combination with any of the XML parsers. So this is after all a non-concern. If you just want to go for best memory efficiency and performance, go for VTD-XML.
I have commented above as well, because there are few options to consider - but by the sound of it your initial description I think you could get away with a simple SAX processor here: which will probably run faster (although it might not look as pretty when it comes to mapping the Java class) than other mechanisms:
There is an example here, which matches quite closely with your example:
http://www.informit.com/articles/article.aspx?p=26351&seqNum=6
Avoid anything that is a DOM parser - no need for that, especially with a large-ish file and relatively simple XML syntax.
Which specific one to use, sorry, I haven't used them, so I can't give you any more guidance than to look at your licensing, performance, and support (for questions).
My favorite XML library is Dom4j
Whenever I have to deal with XML I just use XMLBeans. It may be overkill for what you are after, but it makes life easy (once you know how to use it).
If you don't care about performance at all, Apache Digester may be useful for you, as it will already initialize the Java objects for you after you define the rules.
精彩评论