5

i want to do read simple XML file .i found Simple way to do Xml in Java

There are also several parsers available just wanted to make sure that what are the advantages of using XOM parser over suns parser

Any suggestions?

Community
  • 1
  • 1
anish
  • 1,035
  • 4
  • 13
  • 27

3 Answers3

6

XOM is extremely quick compared to the standard W3C DOM. If that's your priority, there's none better.

However, it's still a DOM-type API, and so it's not memory efficient. It's not a replacement for SAX or STAX.

skaffman
  • 398,947
  • 96
  • 818
  • 769
4

You might want to check this question about the best XML library and its top (XOM) answer; lots of details about advantages of XOM. (Leave a comment if something is unclear; Peter Štibraný seems to know XOM inside and out.)

As mentioned, XOM is very quick and simple in most tasks compared to standard javax.xml. For examples, see this post in a question about the simplest way to read in an XML file in Java. I collected some nice examples that make XOM look pretty good (and javax.xml rather clumsy) there. :-)

So personally I've come to like XOM after evaluating (as you can see in the linked posts); for any new Java project I'd most likely choose XOM for XML handling. The only shortcoming I've found is that it doesn't directly support streaming XML (unlike dom4j where I'm coming from), but with a simple workaround it can stream just fine.

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
  • I see that your question http://stackoverflow.com/questions/967288/how-to-stream-xml-data-using-xom has an accepted answer. Is streaming still a shortcoming with XOM? – Sundeep Jul 11 '13 at 18:22
  • Well, yes, as the XOM API doesn't directly support it. (But the workaround is quite straightforward, as that answer shows.) Anyway, go ahead and try if XOM works for you! Another Java XML lib I would now use is [Simple Framework](http://stackoverflow.com/a/17041986/56285). – Jonik Jul 20 '13 at 14:10
  • I've ended up using XOM with streaming from the very answer, thanks to you. I was able to stream XML with 10000 nodes without hiccup. I've asked because the above statement says `XOM doesn't really support streaming`, while actually XOM only doesn't provide it by default, which I found misleading. I guess you should change that in the above answer. Thanks for the Simple Framework recommendation, will try it in my next project. – Sundeep Jul 21 '13 at 08:06
  • @Sundeep: Fair enough, I tweaked the wording. Glad to hear XOM did the job for you. Cheers. :) – Jonik Oct 19 '13 at 01:50
1

How do you need to access your data?

If it is one-pass, then you don't need to build the tree in memory. You can use SAX (fast, simple) or StAX (faster, not quite so simple).

If you need to keep the tree in memory to navigate, XOM or JDOM are good choices. DOM is the Choice Of Last Resort, whether it is level 1, 2, or 3, with or without extensions.

Xerces, which is the parser included with Java (although you should get the updated version from Apache and not use the one bundled with Java, even in 6.0), also has a streaming native interface called XNI.

If you want to hook other pre-made parts up in the chain, often SAX or StAX work well, since they might build their own model in memory. For example, the Saxon XSLT/XQuery engine works with DOM, SAX or StAX, but builds internally a TinyTree (default) or DOM (optional). DataDirect XQuery works with SAX, StAX or DOM also, but really likes StAX.

lavinio
  • 23,931
  • 5
  • 55
  • 71
  • 4
    Odd that you say SAX is simpler than StAX. SAX may be a simpler API, but writing StAX-based code is an awful lot easier than writing against SAX. – skaffman Jun 16 '09 at 13:50