2015-06-14 68 views
0

我已經獲得了要轉換爲Java對象的XML文件。使用JAXB將XML轉換爲Java對象

我看過JAXB,但是這個XML看起來太複雜了。

這裏store具有相同的元素嵌套到對方。它們不匹配,我不確定如何爲此創建註記類。

<?xml version="1.0" encoding="UTF-8"?> 
<store id="1" name="main"> 
    <store id="2" name="xx"> 
     <location>here</location> 
    </store> 
    <store id="3" name="xx"> 
     <location>here</location> 
    </store> 
    <store id="56" name="xx"> 
     <store id="97" name="xx"> 
      <img>store_image.png</img> 
      <store id="101" name="five"> 
       <img>tore_image.png</img> 
       <store id="145" name="xx"> 
        <img>tore_image.png</img> 
        <location>here</location> 
       </store> 
       <store id="252" name="xx"> 
        <img>store_image.png</img> 
        <location>here</location> 
       </store> 
      </store> 
     </store> 
    </store> 
</store> 

回答

0

可以嘗試

@XmlRootElement(name="store") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Store { 

    // this should take care of nested store elements 
    @XmlElement(name="store") 
    private List<Store> stores; 

    @XmlElement(name="location") 
    private String location; 

    @XmlElement(name="img") 
    private String img; 

    @XmlAttribute(name="id") 
    private String id; 

    @XmlAttribute(name="name") 
    private String name; 

    // and include getters and setter 
} 
相關問題