2013-06-24 36 views
0

我試圖處理以下XML映射的XML類:如何將自定義的JAXB

<?xml version="1.0" encoding="UTF-8"?> 
<operation name="GET_NOTES"> 
    <result> 
    <status>Success</status> 
    <message>Notes details fetched successfully</message> 
    </result> 
<Details> 
    <Notes> 
    <Note URI="http://something/24/notes/302/"> 
     <parameter> 
     <name>ispublic</name> 
     <value>false</value> 
     </parameter> 
     <parameter> 
     <name>notesText</name> 
     <value>Note added to the request</value> 
     </parameter> 
     ... 
    </Note> 
    ... 
    </Notes> 
<Details> 
</operation> 

這有許多無用的東西,所以我試圖映射成類似:

public class Notes { 
    public List<Note> notes; 
} 

public class Note { 
    public String notesText; //value of parameter with name notesText 
    public Boolean isPublic; //value of parameter with name ispublic 
} 

JAXB可以這樣做嗎?你會怎麼做?

回答

0

JAXB簡單地忽略了bean中不存在的xmlelement。或者,使用@XmlTransient而不是@XmlElement註釋該字段,並且它也將被跳過。

在你的情況下,你需要編寫三個類,即註釋,註釋和參數。

@XmlRootElement(name="Notes") 
public class Notes { 
    public List<Note> notes; 
} 

@XmlRootElement(name="Note") 
public class Note { 
public parameter param; 
} 

@XmlRootElement(name="parameter") 
public class parameter { 
    public String name; //value of parameter with name notesText 
    public Boolean value; //value of parameter with name ispublic 
} 
+0

will notesText包含參數中value標籤內的字符串嗎? – GriffinHeart