如何使用Jaxb嵌套POJO类在Java中生成XML模型

时间:2017-04-02 19:51:21

标签: java xml jaxb

我正在使用JAXB创建一个xml数据模型,它似乎对非复杂的XML模型非常有效,但是当我进入一个稍微复杂的XML时,JAXB似乎没有也工作。

这是我想要实现的XML代:

<?xml version="1.0" encoding="UTF-8"?>
<SampleRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="Sample.xsd">

    <Name>Sample Name</Name>
    <Description>This is a description</Description>
    <Graph>Graph test</Graph>

    <VerifyAttr>
        <Data>id</Data>
        <Value>32</Value>
    </VerifyAttr>

    <VeirfyXpath>
        <MemXpath>/Root/Name</MemXpath>
        <Value>Mosawi</Value>
    </<VeirfyXpath>>

</SampleRoot>

这是我创建的嵌套Pojo类(我不应该创建嵌套的pojo吗?也许将它们分开?):

package com.sample.model;

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

@XmlRootElement(name="SampleRoot")
public class Sample {
    private String name;
    private String description;
    private String graph;

    public String getName() {
        return name;
    }
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    @XmlElement
    public void setDescription(String description) {
        this.description = description;
    }
    public String getGraph() {
        return graph;
    }
    @XmlElement
    public void setGraph(String graph) {
        this.graph = graph;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public class VerifyAttr {
        private String data;
        private String value;

        public String getData() {
            return data;
        }
        @XmlElement
        public void setData(String data) {
            this.data = data;
        }
        public String getValue() {
            return value;
        }
        @XmlElement
        public void setValue(String value) {
            this.value = value;
        }

    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public class VerifyXpath {
        private String memXpath;
        private String value;

        public String getMemXpath() {
            return memXpath;
        }
        @XmlElement
        public void setMemXpath(String memXpath) {
            this.memXpath = memXpath;
        }
        public String getValue() {
            return value;
        }
        @XmlElement
        public void setValue(String value) {
            this.value = value;
        }

    }

}

这是演示pojo模型的演示:

public static void main(String[] args) {
    Sample sample = new Sample();

    sample.setName("Name");
    sample.setDescription("This is a description");
    sample.setGraph("Graph Test");

    VerifyAttr va = sample.new VerifyAttr();
    va.setData("id");
    va.setValue("32");

    VerifyXpath vx = sample.new VerifyXpath();
    vx.setMemXpath("/Root/Name");
    vx.setValue("Mosawi");


    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Sample.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(sample, new File("Sample.xml") );
        jaxbMarshaller.marshal(sample, System.out);

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

我遇到的问题是没有生成上面定义的完整XML。这就是生成的内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SampleRoot>
    <description>This is a description</description>
    <graph>Graph Test</graph>
    <name>Name</name>
</SampleRoot>

我做错了什么?为什么我不能像上面那样获得模型XML?问题出在demo main

1 个答案:

答案 0 :(得分:0)

好的,所以我能够通过阅读一些JaxB文档来解决这个问题。我已经将POJO解耦为3个类并调整了Jaxb注释:

Sample.java VerifyAttr.java VerifyXpath.java

<强> VerifyAttr.java

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(propOrder={"dataAttr","valueAttr"})
public class VerifyAttr {
    //...
}

<强> VerifyXpath.java

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(propOrder={"dataXpath","valueXpath"})
public class VerifyAttr {
    //...
}

和根节点的顶级:

<强> Sample.java

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class VerifyAttr {
    @XmlElement(name="VerifyAttr", type=VerifyAttr.class)
    private ArrayList<VerifyAttr> va;

    @XmlElement(name="VerifyXpath", type=VerifyXpath.class)
    private ArrayList<VerifyXpath> vx;

    // some other fields
    //..getter and setters
}

希望这有助于其他新手Jaxb