0

Is it possible to marshal java classes (annotated with JAXB annotations) by StAX without using JAXB itself?

1 Answers1

2

No, to marshall with JAXB annotations, you need JAXB - that's what it does. Yes, you can use StAX as the output writer when you serialize a JAXB object tree using a Marshaller.

They are two separate things. StAX is not faster than JAXB, it does something different - it is needed to create textual XML after JAXB has generated the correct XML events to tell StAX what the XML should look like.

Use the following method on javax.xml.bind.Marshaller to send your JAXB objects to StAX:

/**
 * Marshal the content tree rooted at <tt>jaxbElement</tt> into a
 * {@link javax.xml.stream.XMLStreamWriter}.
 *
 * @param jaxbElement
 *      The content tree to be marshalled.
 * @param writer
 *      XML will be sent to this writer.
 *
 * [...]
 * @since JAXB 2.0
 */
public void marshal( Object jaxbElement, javax.xml.stream.XMLStreamWriter writer )
    throws JAXBException;
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • Thanks, but is there a way to marshal java object by StAX automatically (without using Cursor API or Iterator API)? – Denis Fahritdinov Jan 31 '14 at 11:33
  • No, StAX works at a different level of abstraction. For StAX, your data model already needs to work in terms of the things that you find in an XML file - an element, and attribute, a text node, etc. Java *objects* in general, unless that directly represent XML, need to be mapped using some tool, and that is not within the scope of the StAX API. – Erwin Bolwidt Jan 31 '14 at 11:57