简体   繁体   中英

How add element to xml file by JAXB?

I have problem when I want add element to my XML,so I have two element project this root element and layer this element is inside element project, but I can't add layer to project element (user can add project then add more one layers),I need method layer inside element Project same my XML.

This Project class :

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

    @XmlElement(name = "Layer")
    protected List<Layer> layer;
    @XmlAttribute(name = "name")
    protected String name; 

   public List<Layer> getLayer() {
        if (layer == null) {
            layer = new ArrayList<Layer>();
        }
        return this.layer;
    }
    public String getName() {
        return name;
    }
    public void setName(String value) {
        this.name = value;
    }
}

This layer class :

@XmlAccessorType(XmlAccessType.FIELD)
public class Layer {
    @XmlElement(name = "LayerName", required = true)
    protected String layerName;
    @XmlAttribute(name = "idLayer")
    protected int idLayer;
    public String getLayerName() {
        return layerName;
    }
    public void setLayerName(String value) {
        this.layerName = value;
    }
    public int getIdLayer() {
        return idLayer;
    }
    public void setIdLayer(int i) {
        this.idLayer = i;
    }
}

This My method for insert project :

 public void Insert(Project entity) {
    Project pr = new Project();
    pr.setName("pr1");
        try {
    JAXBContext jc = JAXBContext.newInstance(Project.class);
    javax.xml.bind.Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    File XMLfile = new File("Projects" + "//" + pr.getName() + "//" +"ProjectDataBase.xml");
    marshaller.marshal(pr, XMLfile);
    }
    } catch (JAXBException e) {
         e.printStackTrace();
    }
}

My XML :

   <Project name="pr1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <Layer idLayer="0">
         <LayerName>LayerName1</LayerName>
       </Layer> 
       <Layer idLayer="1">
         <LayerName>LayerName2</LayerName>
       </Layer> 
    </Project>

In your insert method, you don't have any layer so you just have to add some layer.

you can do something like follows:

public void insert(Project entity) {
    Project pr = new Project();
    pr.setName("pr1");
    //-Create a Layer-
    Layer testLayer = new Layer();
    testLayer.setIdLayer(1);
    testLayer.setLayerName("Layer 1");
    //Add that layer to your Project's list by first getting it and then calling add method on it.
    pr.getLayer().add(testLayer);
    try {
        JAXBContext jc = JAXBContext.newInstance(Project.class);
        javax.xml.bind.Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(marshaller.JAXB_FORMATTED_OUTPUT,
                Boolean.TRUE);
        File XMLfile = new File("Projects" + "//" + pr.getName() + "//" +"ProjectDataBase.xml");
        marshaller.marshal(pr, XMLfile);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

The above modification will generate you following xml.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Project name="pr1">
    <Layer idLayer="1">
        <LayerName>Layer 1</LayerName>
    </Layer>
</Project>

Apart from that, Please follow java coding conventions: Method name should start with lower case.

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