2017-06-01 65 views
0

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

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

感谢, 米哈尔

+0

你有没有检查这些链接https://stackoverflow.com/questions/14789302/parse-xml-to-java-pojo-in-efficient-way https://stackoverflow.com/questions/1651924/simple-java -xml到POJO映射结合 –

回答

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

您将propably需要一些更多的类来读你的原始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); 
} 

这应该会给你的对象来处理。