简体   繁体   中英

How to avoid root element annotation while marshalling in JAXB?

I want to store my object data to XML. Below code spinets will show the example of model class.

Class Model
{
  @XmlElement
  private int id;
  @XmlElement
  Private string name;
} 

I will have multiple model objects which will be stored in some list as below

@XmlRootElement
Class ModelWrapper
{
  @XmlElement
  @XmlJavaTypeAdapter(value = ListAdapter.class, type = List.class)
  List<model> list;
  public setlist(List<model>list)
  {
    //setting list
  }
  public List<model> getlist()
  {
    return list
  }
}

Now if I marshall this using JAXB, something like this will be produced:

<Modelwrapper>
  <List>
  ......
  ......
  </List>
</Modelwrapper>

I want to avoid one of the root may be list or Modelwrapper. Is there any way to do it?

In this way your xml will be

<ModelWrapper>
   <model>
     ......
   </model>
   <model>
     ......
   </model>
</ModelWrapper>

ModelWrapper.java

@XmlRootElement
public class ModelWrapper {

    @XmlElement(name = "model")
    protected List<Model> list;

Test

ModelWrapper.java

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ModelWrapper", propOrder = {
    "list"
})
public class ModelWrapper {

    @XmlElement(name = "model")
    private List<Model> list;

    public List<Model> getList() {
        return list;
    }

    public void setList(List<Model> list) {
        this.list = list;
    }

}

Model.java

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Model", propOrder = {
    "id","name"
})
public class Model {
    @XmlElement
    private int id;
    @XmlElement
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

Main.java

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {

    public static void main(String[] args) throws JAXBException {

        JAXBContext jc =  JAXBContext.newInstance(ModelWrapper.class);

        ModelWrapper mw = new ModelWrapper();

        List<Model> list = new ArrayList<Model>();
        Model m = new Model();
        m.setId(1);
        m.setName("model1");
        list.add(m);
        m = new Model();
        m.setId(1);
        m.setName("model2");
        list.add(m);

        mw.setList(list);

        Marshaller mar = jc.createMarshaller();

        mar.marshal(mw, System.out);


    }

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<modelWrapper>
    <model>
        <id>1</id>
        <name>model1</name>
    </model>
    <model>
        <id>1</id>
        <name>model2</name>
    </model>
</modelWrapper>

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