Can Jaxb marshal child elements without the root element? Can Jaxb marshal child elements without the root element? xml xml

Can Jaxb marshal child elements without the root element?


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 : )


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

StringWriter stringWriter = new StringWriter();JAXB.marshal(bbb, stringWriter);String bbbString = stringWriter.toString();