如何通过JAXB将元素添加到xml文件中?

时间:2014-04-05 11:01:42

标签: java xml jaxb

我想在我的XML中添加元素时遇到问题,所以我有两个元素项目这个根元素和图层这个元素在元素项目中,但是我不能将图层添加到项目元素中(用户可以添加项目然后添加更多一层),我需要元素Project里面的方法层和我的XML相同。

此项目类:

@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;
    }
}

此图层类:

@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;
    }
}

这是插入项目的My方法:

 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();
    }
}

我的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>

1 个答案:

答案 0 :(得分:0)

在您的插入方法中,您没有任何图层,因此您只需添加一些图层。

您可以执行以下操作:

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();
    }
}

以上修改将生成以下xml。

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

除此之外,请遵循java编码约定:方法名称应以小写字母开头。