XML到POJO - 没有map xml属性

时间:2017-06-01 04:52:25

标签: java json pojo

我有这样的xml,我想把它读到POJO。 如何在不读取属性的情况下创建POJO: Admin,time,dirtyId ..? 我应该添加哪些json注释(用Java编写的代码)?

<response status="success" code="19">
    <result total-count="1" count="1">
        <rules admin="admin" dirtyId="39" time="2017/05/28 11:35:18">
        <entry name="Shared" admin="admin" dirtyId="33" time="2017/04/03 15:24:03">
                <source admin="admin" dirtyId="33" time="2017/04/03 15:24:03">
                    <member admin="admin" dirtyId="33" time="2017/04/03 15:24:03">ip-10.30.14.14</member>
                </source>
                <destination admin="admin" dirtyId="33" time="2017/04/03 15:24:03">
                    <member admin="admin" dirtyId="33" time="2017/04/03 15:24:03">ip-10.30.14.25</member>
                </destination>
            </entry>
</rules>
    </result>

这是POJO代表xml中的条目:

public class PanoramaRule {
    private PanoramaRuleOptions option;
    private String name;
    private List<String> to;
    private List<String> from;
    private List<String> source;
    private List<String> destination;
    @JsonProperty("source-user")
    private List<String> sourceUser;
    private List<String> category;
    private List<String> application;
    private List<String> service;
    @JsonProperty("hip-profiles")
    private List<String> hipProfiles;
    private String action;
    private String description;
    private Boolean disabled;

    public void setDisabled(String disabled) {
        this.disabled = "yes".equals(disabled);
    }
}

谢谢, 米甲

1 个答案:

答案 0 :(得分:0)

我个人认为没有简单的方法将此xml映射到您的班级。最简单的方法是读取xml并将属性手动放入类中。但你应该重新设计你的类,因为它不是真正的面向对象。不同的标签显然应该是单独的类。您不应该只将属性放入列表而不是使用类。

要读取xml文件,jaxb通常是一种简单的方法。我用一小部分xml和我自己的类做了一个简单的例子。你会弄明白其余的。要使用此示例,您需要在类路径上使用jaxb。

小xml:

<response status="success" code="19">
    <result total-count="1" count="1">
    </result>
</response>

我将值拆分为不同的类:

 public class Result {

    private int totalCount;
    private int count;

    /* The name Attribute defines the tag name in the xml */
    @XmlAttribute(name = "total-count")
    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    @XmlAttribute
    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

/* Marker that this can be the first tag in your xml */
@XmlRootElement
public class Response {

    private String status;

    private int code;

    private Result result;

    /* <response status="success" ... is an attribute */
    @XmlAttribute
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @XmlAttribute
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    /* <result> is an element which can have it's own attributes */
    @XmlElement
    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }
}

您可能需要更多的课程来阅读原始的xml。用于读取文件的示例代码:

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

    final JAXBContext instance = JAXBContext.newInstance(Response.class);
    final Unmarshaller unmarshaller = instance.createUnmarshaller();

    final Response result = (Response) unmarshaller.unmarshal(new File("sample.xml"));

    System.out.println(result);
}

这应该为您提供合作对象。