Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Jaxb marshal child elements without the root element?

I'm not sure if the following question is possible with jaxb, but I'll ask anyway.

In a certain project, we're using jaxb with a defined schema to create the next structure of xml file.

<aaa>
     <bbb>
        more inner children here
     </bbb>
     <bbb>
        more inner children here
     </bbb>
</aaa>

We're also using the automatic class generating of jaxb which creates the classes: aaa and bbb, where aaa was generated as the @XmlRootElement.

We now want to use the same schema in a new project, that will be also compatible with the previous project. What I would like to do, is to use the same jaxb generated classes, without performing any changes in the schema in order to marshal only a single bbb object into xml.

JAXBContext jc = JAXBContext.newInstance("generated");
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(bbb, writer);

So we would get the next result:

 <bbb>
    <inner child1/>
    <inner child2/>
    ...
 </bbb>

I'm currently not able to do so as the marshaller yells that I do not have a @XmlRootElement defined.

We're actually trying to avoid the case of separating the schema into 2 schemas, one of only bbb and the other where aaa imports bbb.

Thanks in advance!

like image 572
Meny Issakov Avatar asked Aug 06 '12 06:08

Meny Issakov


People also ask

How does JAXB marshalling work?

In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.

What is JAXB marshalling?

The JAXB Marshaller interface is responsible for governing the process of serializing Java content trees i.e. Java objects to XML data. This marshalling to XML can be done to variety of output targets.

How do you Unmarshal string JAXB?

To unmarshal an xml string into a JAXB object, you will need to create an Unmarshaller from the JAXBContext, then call the unmarshal() method with a source/reader and the expected root object.


1 Answers

I am maybe late with 3 years but have you ever tried something like that :

public static String marshal(Bbb bbb) throws JAXBException {
    StringWriter stringWriter = new StringWriter();

    JAXBContext jaxbContext = JAXBContext.newInstance(Bbb.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    // format the XML output
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    QName qName = new QName("com.yourModel.bbb", "bbb");
    JAXBElement<Bbb> root = new JAXBElement<Bbb>(qName, Bbb.class, bbb);

    jaxbMarshaller.marshal(root, stringWriter);

    String result = stringWriter.toString();
    LOGGER.info(result);
    return result;
}

Here is the article I use when I have to marshal/unmarshal without rootElement : http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html

It works pretty fine for me. I am writing this response for other lost souls searching for answers .

All the best : )

like image 196
Lazar Lazarov Avatar answered Sep 21 '22 17:09

Lazar Lazarov