This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java and xml parser

 
christophe seguinot
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have an xml file which contains hundred of records. I need to store the java objects associated to these datas in a database. Which parser do you think I should use ? JAXB, SAX .... ??

Thanks a lot.

Chris
 
Campbell Ritchie
Sheriff
Posts: 48407
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this topic would sit better on our XML forum. Moving thread.
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use the parser which is most convenient for you to program with, for such a small problem. Just pick one and get it done.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also would depend on the operation you would be doing with your XML- Manipulating the XML or just parsing the XML. Depending on that you can choose either a DOM based parser or an event based parser.
 
Leon Omk
Ranch Hand
Posts: 75
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


JAXB is my favorite, simple, elegant and pain-free, as good as castor.
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, JAXB is great as long as you have done the up-front design and created the schemas which make it work. But we don't know if that was done for Christophe's XML file.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Yeah, JAXB is great as long as you have done the up-front design and created the schemas which make it work. But we don't know if that was done for Christophe's XML file.

Agree with Paul. You would require the schema to validate the XML.
 
Drew Liscomb
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Paul and Mohammed said, the kind of parser you use depends on your data, and what you want to do with it.

If you are "importing" an XML file with many "records" into a database, then you probably want an "event-based parser" like SAX or StAX. You can iterate through the file quickly, transferring the data into database tables. I see these parsers as more low-level, "close to the byte" tools to be used when you really want to crank through a boatload of data.

If you want to do more than this, then more object-based toolkits may be in order, like JAXB, XMLBeans, and Castor. Using these tools you end up with a Java object with the data from the XML. You can then do what you like with the data. One list is here: http://java-source.net/open-source/xml-parsers

I have used XMLBeans with good success, while JAXB would not compile the schema I needed. And yes, in my case, I have no control over the schema (.XSD and .WSDL files), and even less control over how well my source of XML conforms to it. Having said that, the "lazy object creation" claims I have seen about XMLBeans have not held up for me, and trying to read a large (~800MB) XML document used up all available memory and crashed.

Kind regards,
Drew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic