Java unmarshal xml元素

时间:2015-02-11 11:14:43

标签: java xml jaxb

我只是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>

我需要获取元素中的start,count,total。 我有两个类文件 列出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);

如何在主函数

中获取start,end,total的元素值

1 个答案:

答案 0 :(得分:1)

您必须创建一个建模<result> XML标记的Java类,以及一个建模<listtype> XML标记的类。通常,您使用@XmlElement注释Java类属性以指示它们来自XML标记的值,您可以使用@XmlAttribute来指示Java属性属于XML标记的属性:< / p>

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作为来源而不是{{{{}}来解组它1}}:

File

如果您使用的是String s = "<result>...</result>"; // XML content as a String // Unmarshal: 1 line only Result r = JAXB.unmarshal(new StringReader(s), Result.class); StringBuffer StringBuilder,则可以使用sb方法将其转换为String

toString()