0

I am trying to parse my giant xml using SAX as well as DOM parser. DOM parser is taking lots of memory and implementation of SAX parseh is making me check some nested tags with their names,taking lots of time and does not look efficient.

I parsed xml using JAXB and made java objects of it and I am getting values from getters .(The classes are generated using XSD of my xml file)

JAXBContext jaxbContext = JAXBContext.newInstance(Myclass.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

InputStream in = new ByteArrayInputStream(xmldata.getBytes()); Myclass classobject = (Myclass) jaxbUnmarshaller.unmarshal(in);

The classobject is giving what I want.

My project is not webservice project.So my question is : Is it recommended using JAXB for Java Projects?I am technically nor parsing xml(JAXB comes with DOM and SAX) ,so it unmarshlling is parsing?

3 Answers 3

1

Yes JAXB can be (and regularly is) used in Java SE for non-Web Service applications. It is included in Java SE 6.

The correct XML processor for you (DOM, SAX, StAX, JAXB, XPath, etc) depends on what you are trying to build. Also it doesn't need to be an "either or" choice as many of these APIs are designed to work together.

Sign up to request clarification or add additional context in comments.

Comments

1

There are people who are very enthusiastic about data binding (JAXB) and there are people who are very reluctant to use it. In my experience, it works well when the data is highly structured and the structure is very stable; it's a nightmare if the vocabulary is large, if it contains mixed content, or if it changes every couple of months. I don't have any experience of using it with very large data sets. My personal preference is wherever possible to process XML data using XML-based processing languages, that is, XSLT and XQuery, rather than with languages such as Java that are a poor fit to the XML data model.

3 Comments

Just a shame all this is good stuff is being thrown under the wheels of the JSON juggernaut, which is worse in almost every way
Don't believe the idle chatter. My XML products are seeing a 30% annual growth in usage, and I'm sure they're not alone.
Thanks ,would like to try XSLT,XQUery
0

There are alternatives. If you're less interested in first transforming the XML into a statically typed structure (and you can fight off the engineers that tell you thats the only way to write Java) you could try a dynamic approach.

Ie Java 8 library Dynamics.

XmlDynamic example = new XmlDynamic("<products>" +
    "<product name=\"some product\">" +
      "<created>2012-04-02T12:34</created>" +
    "</product>" +
    "<product name=\"some newer product\">" +
      "<created>2016-01-02T00:03:45.234Z</created>" +
    "</product>" +
  "</products>");

String firstProductName = example.get("products|product|@name").asString();
// "some product"

ZonedDateTime secondProductCreation = example.get("products|product[1]|created")
    .convert().intoZonedDateTime();
// java.time.ZonedDateTime 2016-01-02T00:03:45.234Z

Sometimes it's benefitial to use dynamic concepts when working with dynamic data (xml). Null safety and fluent casting & conversion and descriptive error messages are very helpful too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.