开发者

Which Java XML Parser for simple processing?

开发者 https://www.devze.com 2023-02-14 17:53 出处:网络
I am producing some XML in a java application. I am looking at the variety of XML parsing options. I am not intendi开发者_高级运维ng to do anything more than traverse the structure and extract values

I am producing some XML in a java application. I am looking at the variety of XML parsing options. I am not intendi开发者_高级运维ng to do anything more than traverse the structure and extract values out of it. I need to use one of them that is built into the Java API (1.5+) without any additional plugins. I don't need to create "events" or transform it into anything else. I am not producing XML, merely reading and extracting data. I am not enforcing a schema either.

Sun provide a list here, but it's not really obvious what I should use.

http://java.sun.com/developer/technicalArticles/xml/JavaTechandXML/

What would be the most appropriate XML API to use in this case ? JAXP ? JDom ? XPath ?


I think using a DOM parser to parse the XML and load it into memory in a Document sounds sufficient for your needs.

You wouldn't use XPath in that case, just the Document API.

JAXP is just a synonym for the XML parsing technology build into the JDK. The term JAXP (P is for Parsing) distinguishes it from JAXB (B is for binding).

Some 3rd party libraries built on top of DOM might make your life easier. Think about JDOM or DOM4J.


The most classical way of doing things in IMO would be combination of JAXP and XPath. Java 5.0 includes JAXP 1.3 and this is standard stuff. Please see this answer to a similar question for a minimalist coding sample.


DOM parser is what you looking for i think. easy to implement it and it has fast searching node capability


As the parsing strategy you can use either DOM strategy which has the advantage that the hole document is kept in memory and you can access it via xpath. i recommend this if you have small xml documents or if you really NEED all the data to be present and accessable all the time because this consumes a lot of heap space.

if you have bigger documents or if you dont need to access the all the time you should either use the SAX method or the Stax method (xml pull parsing) if this is available in your java distribution. These methods are event based. so they traverse through the xml tree and make a kind of callback to a class defined by you. so you can react on events like "element xy starts" "element xy ends"


Using the standard DOM Parser is good enough for your purpose. Try out this example.


I think that the most practical tool to use is XStream, from ThoughtWorks. Some modern mvc frameworks like VRaptor use it to serve and consume xml. Take a look at: http://x-stream.github.io/


XOM.

Use xpath.


If it is very trivial - do it in SAX parser.


It seems that SAX is the API you want.

Google "SAX Parsing" and you will find many examples.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号