簡體   English   中英

Java Unmarshall xml元素

[英]Java unmarshall xml element

我只是Java的初學者。 我有一個xml響應如下。我想從響應中提取元素

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

我需要獲取元素的開始,總數。 我有兩個用於此List類型的類文件。

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

如何在主要功能中獲取開始,結束,總計的元素值

您必須創建一個對<result> XML標記建模的Java類,以及一個對<listtype> XML標記建模的類。 通常,您使用@XmlElement注釋Java類屬性以指示它們來自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

編輯:

如果您的XML以StringBufferStringBuilderString ,則可以通過創建StringReader作為源而不是File來取消編組:

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

如果您在名為sbStringBufferStringBuilder擁有它,則可以使用其toString()方法將其轉換為String

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM