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);
我如何在主開始,結束,總的元素值函數
我將XML響應分配給字符串緩衝區。我將不得不從字符串緩衝區解除編碼 – Karthi 2015-02-11 17:48:38
@Karthi查看我編輯的答案以從「String」或「StringBuffer」解組。 – icza 2015-02-11 18:38:02
嗨@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