2017-06-01 64 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); 
} 

這應該會給你的對象來處理。