1

I Have a problem with marshalling the code as expected XML

public void xmleg() throws XMLStreamException
    {


     XMLOutputFactory factory = XMLOutputFactory.newInstance();
     XMLStreamWriter writer = factory.createXMLStreamWriter(System.out);
     writer.writeStartDocument();
     writer.writeStartElement("Zoos1"); 


         QName q=new QName("","Zoo"); 
         for(Zoo add: zoo_list)
        {
        try 
         {

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



             jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
             jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
             jaxbMarshaller.marshal(new JAXBElement<Zoot>(q,Zoo.class,add),System.out);

         } catch (JAXBException e) {
            e.printStackTrace();
              }
        }
        writer.writeEndDocument();
         writer.close();
        }


}

Have used the XMLStreamWriter to print the tags required as default..But all the write statements are printed at the last Start tag,start element as well.

The generated output is:

<Zoo>
    <linkId>0</linkId>
    <name>fjjfjfrj</name>

</Zoo>

<Zoo>
    <linkId>0</linkId>
    <name>fgjfjfj</name>
</Zoo>
<?xml version="1.0" ?><Zoos></Zoos>

The expected output should be:

<?xml version="1.0" ?>

<Zoo>
    <linkId>0</linkId>
    <name>fjjfjfrj</name>
    </Zoo>

 <Zoo>
    <linkId>0</linkId>
    <name>fgjfjfj</name>
</Zoo>

user2224083
  • 33
  • 1
  • 4

1 Answers1

2

For your use case where you are using StAX to add a root element, you will also need to have JAXB marshal to the XMLStreamWriter. When marshalling to the middle of a document you need to ensure that you set the JAXB_FRAGMENT property. Below is an example:

import javax.xml.bind.*;
import javax.xml.stream.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Zoo.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);

        xsw.writeStartDocument();
        xsw.writeStartElement("zoos");

        Zoo zoo1 = new Zoo();
        zoo1.linkId = 1;
        zoo1.name = "foo";
        marshaller.marshal(zoo1, xsw);

        Zoo zoo2 = new Zoo();
        zoo2.linkId = 2;
        zoo2.name = "bar";
        marshaller.marshal(zoo2, xsw);

        xsw.writeEndElement();
        xsw.writeEndDocument();
        xsw.close();
    }

}

When marshalling to an XMLStreamWriter the output won't be formatted.

<?xml version="1.0" ?><zoos><zoo><linkId>1</linkId><name>foo</name></zoo><zoo><linkId>2</linkId><name>bar</name></zoo></zoos>

For help on formatting StAX output check out the answers to the following question:

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400