2015-02-11 79 views
1

我只是java的初學者。我有一個XML響應,follows.I要從響應Java unmarshall xml元素

<result> 
<status>success</status> 
<function>get_list</function> 
<controlid>testControlId</controlid> 
<listtype start="0" end="9" total="3463">arpayment</listtype> 
<data> 
</data> 
</result> 

我需要開始,算上,總的元素提取元素。 我對這個 名單type.class兩類文件

import javax.xml.bind.annotation.XmlAttribute; 
    import javax.xml.bind.annotation.XmlValue; 

    public class Listtype 
    { 
    @XmlAttribute 
    public 
    Integer start; 
    @XmlAttribute 
    public 
    Integer end; 
    @XmlAttribute 
    public 
    Integer total; 
    @XmlValue 
    String value; 

Result.class 

    public class Result 
    { 
    @XmlElement 
    String status; 
    @XmlElement 
    String function; 
    @XmlElement 
    String controlid; 
    @XmlElement 
    public 
    Listtype listtype; 

這是我如何處理我的XML

String body = <XMLREQUEST> 
StringBuffer response = null; 
HttpURLConnection connection; 
Object endPoint = "https://XXXX.phtml"; 
URL obj = new URL((String) endPoint); 
connection = (HttpURLConnection) obj.openConnection(); 
//add request header 
connection.setRequestMethod("POST"); 
connection.setRequestProperty("Content-Type", "XML"); 
String urlParameters = body; 
System.out.println(urlParameters); 
connection.setDoOutput(true); 
DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); 
wr.writeBytes(urlParameters); 
wr.flush(); 
wr.close(); 
if (connection.getResponseCode() != 200) { 
throw new RuntimeException("Failed : HTTP error code : " 
+ connection.getResponseCode()); 
} 
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream())); 
String inputLine; 
response = new StringBuffer(); 
while ((inputLine = in.readLine()) != null) { 
response.append(inputLine); 
} 
in.close(); 
Result r = JAXB.unmarshal(new StringReader(response.toString()), Result.class); 
System.out.println("\tListtype start: " + r.listtype.start); 
System.out.println("\tListtype end : " + r.listtype.end); 
System.out.println("\tListtype total: " + r.listtype.total); 

我如何在主開始,結束,總的元素值函數

回答

1

您必須創建一個建模<result> XML標記的Java類,以及建模<listtype> XML標記的類。通常你註釋的Java類@XmlElement屬性,以表明它們是從XML標籤的值,可以使用@XmlAttribute,表明了Java屬性是XML標記的屬性:

class Listtype { 
    @XmlAttribute 
    Integer start; 
    @XmlAttribute 
    Integer end; 
    @XmlAttribute 
    Integer total; 
    @XmlValue 
    String value; 
} 

class Result { 
    @XmlElement 
    String status; 
    @XmlElement 
    String function; 
    @XmlElement 
    String controlid; 
    @XmlElement 
    Listtype listtype; 
} 

你可以使用JAXB類這樣的解組結果XML(這裏我以爲XML數據文件"result.txt"):

// Unmarshal: 1 line only 
Result r = JAXB.unmarshal(new File("result.xml"), Result.class); 

// The rest is just printing it to the console: 
System.out.println("Status : " + r.status); 
System.out.println("Function : " + r.function); 
System.out.println("Controlid: " + r.controlid); 
System.out.println("Listtype : " + r.listtype.value); 
System.out.println("\tListtype start: " + r.listtype.start); 
System.out.println("\tListtype end : " + r.listtype.end); 
System.out.println("\tListtype total: " + r.listtype.total); 

輸出:

Status : success 
Function : get_list 
Controlid: testControlId 
Listtype : arpayment 
    Listtype start: 0 
    Listtype end : 9 
    Listtype total: 3463 

編輯:

String s = "<result>...</result>"; // XML content as a String 
// Unmarshal: 1 line only 
Result r = JAXB.unmarshal(new StringReader(s), Result.class); 

如果:

如果你的XML是存在於StringBufferStringBuilder或作爲String,你可以通過創建一個StringReader作爲源,而不是一個File解編它你可以用它的StringBufferStringBuilder命名爲sb,你可以使用它的toString()方法將它轉換成String方法:

Result r = JAXB.unmarshal(new StringReader(sb.toString()), Result.class); 
+0

我將XML響應分配給字符串緩衝區。我將不得不從字符串緩衝區解除編碼 – Karthi 2015-02-11 17:48:38

+0

@Karthi查看我編輯的答案以從「String」或「StringBuffer」解組。 – icza 2015-02-11 18:38:02

+0

嗨@icza。現在我在線程「main」java.lang.NullPointerException錯誤中得到異常:System.out.println(「\ tListtype start:」+ r.listtype.start); System.out.println(「\ tListtype end:」+ r.listtype.end); System.out.println(「\ tListtype total:」+ r.listtype.total); – Karthi 2015-02-12 12:35:43