简体   繁体   中英

How do I output a collection of objects with JAXB?

Not able to generate the required XML from the java class as expected.

This is a class defining the property of zoo

**//Modal Class**
 public class Zoo 
{
private String name;
private String place;
    //Getters Setters

}

Action class with list of zoo class to be in XML

//Action Class with multiple objects of zoo class

@ManagedBean
@XmlRootElement
public @SessionScoped class zoos implements Serializable {
   private String name;
   private String place;
   private static final ArrayList<Zoo> zoo_list
= new ArrayList<Zoo>();

    @XmlElement
public ArrayList<Zoo> getZoo_list()
{
  return zoo_list;

}
    public void xmleg()
    {

        File file = new File("C:\\file.xml");   
        for(Zoo add: zoo_list)
        {
         try 
         {
            JAXBContext jaxbContext = JAXBContext.newInstance(Zoos.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();


            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);


            jaxbMarshaller.marshal(new JAXBElement<Zoo>(new QName("","Zoo"),zoo.class,add),file);
            jaxbMarshaller.marshal(new JAXBElement<Zoo>(new QName("","Zoo"),zoo.class,add),System.out);

              } catch (JAXBException e) {
            e.printStackTrace();
              }
         }
        }

Output that is generated is:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<zoo>
    <linkId>0</linkId>
    <name>gfdsgdgtretr</name>
    <place>gdfg</place>
</zoo>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<zoo>
   <linkId>0</linkId>
    <name>gfdsgdgtretr</name>
    <place>gdfg</place>
</zoo>

But the Expected Output should be:

 <zoos>
   <zoo>
     <linkId>0</linkId>
     <name>gfdsgdgtretr</name>
     <place>gdfg</place>
  </zoo>
  <zoo>
    <linkId>0</linkId>
    <name>gfdsgdgtretr</name>
    <place>gdfg</place>
  </zoo>     
  </zoos>

Have tried with the @XMLElementWrapper annotation as well but its not working too.Have gone through many tutorials but didnt find the solution.

UPDATE

I just reread your question and you already have the Zoos class. You should be marshalling the instance of that instead of the individual items from the zoo_list property. You can change the element name that property maps to with the @XmlElement annotation.

@XmlElement(name="zoo")
public ArrayList<Zoo> getZoo_list()

JAXB (JSR-222) implementations require a root objject. The easiest thing to do is create an object called Zoos that holds onto the list of Zoo objects and marshal that.

@XmlRootElement
public class Zoos {

    private List<Zoo> zoo;

    public List<Zoo> getZoo() {
        return zoo;
    }

    public void setZoo(List<Zoo> zoo) {
        this.zoo = zoo;
    }

}

Alternatively, you could create a FileWriter for the File . Then you could write the start element yourself. Then use JAXB to marshal each of the Zoo instances to the FileWriter (you will need to leverage the following property to have JAXB exclude the XML header.

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

Finally you need to close the root element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM