How to add inline xml schema using jaxb?

Is it a good idea to use JAXB to generate XML? Why or why not?

  • I often see situations where JAXB is not flexible enough. For example,  we may want to generate XML dynamically. Some fields contain the data that need to be passed around. But when we return the data to users, we may want to hide the fields determined by request parameters. Since I always see exceptions when using JAXB. I start to wonder if it is a good idea to use JAXB.

  • Answer:

    It is pretty straightforward to modify the output created by the JAXB Marshaller by using one of the versions of the marshal method that outputs to a SAX2 ContentHandler or a JAXP Result instance (which could be backed by DOM, SAX, StAX, or stream-based implementation). If you use one of the stream-based output methods, then you're stuck with whatever JAXB gives you, but not if you use one of the other methods. Whether or not it's a good idea for you to use JAXB in light of the former depends on whether you have any other objections to it. Your basic choice is between an annotation-based framework like JAXB or one based on external code or configuration (e.g. XStream). If you want to use annotations on your objects to drive XML output, then JAXB 2.1 has the benefit that it is bundled with Sun/Oracle's Java 6 JDK. This has the negative aspect of making it a nightmare to use a newer version of JAXB (it involves using the endorsed library path mechanism).

Mike Dillon at Quora Visit the source

Was this solution helpful to you?

Other answers

I like JAXB too, but almost always after considering it for a while during the prototyping phase, I end up using XStream because the flexibility it provides. I end up encountering some situation which requires me to go to all these forums trying to figure out how to do something in JAXB. e.g. how to write a comment.

Jon Blanton

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.