Java解组xml元素
我只是java的初学者。我有一个xml响应,如下所示Java解组xml元素,java,xml,jaxb,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> <da
<result>
<status>success</status>
<function>get_list</function>
<controlid>testControlId</controlid>
<listtype start="0" end="9" total="3463">arpayment</listtype>
<data>
</data>
</result>
这就是我处理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);
字符串体=
StringBuffer响应=null;
httpurl连接;
对象终结点=”https://XXXX.phtml";
URL obj=新URL((字符串)端点);
connection=(HttpURLConnection)obj.openConnection();
//添加请求头
connection.setRequestMethod(“POST”);
setRequestProperty(“内容类型”、“XML”);
字符串参数=主体;
System.out.println(URL参数);
connection.setDoOutput(真);
DataOutputStream wr=新的DataOutputStream(connection.getOutputStream());
writeBytes(URL参数);
wr.flush();
wr.close();
if(connection.getResponseCode()!=200){
抛出新的RuntimeException(“失败:HTTP错误代码:”
+connection.getResponseCode());
}
BufferedReader in=新的BufferedReader(
新的InputStreamReader(connection.getInputStream());
字符串输入线;
响应=新的StringBuffer();
而((inputLine=in.readLine())!=null){
追加(inputLine);
}
in.close();
Result r=JAXB.unmarshal(新的StringReader(response.toString()),Result.class);
System.out.println(“\t列表类型开始:“+r.listtype.start”);
System.out.println(“\t列表类型结束:“+r.listtype.end”);
System.out.println(“\tListtype总计:+r.listtype.total”);
如何在main函数中获取start、end、total的元素值您必须创建一个建模
XML标记的Java类,以及一个建模
XML标记的类。通常使用注释Java类属性以指示它们来自XML标记的值,可以使用来指示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;
}
您可以使用如下类来解组结果XML(这里我假设XML数据位于文件“result.txt”
):
输出:
Status : success
Function : get_list
Controlid: testControlId
Listtype : arpayment
Listtype start: 0
Listtype end : 9
Listtype total: 3463
编辑:
如果您的XML存在于StringBuffer
、StringBuilder
或作为String
中,您可以通过创建一个作为源文件而不是文件来解组它:
String s = "<result>...</result>"; // XML content as a String
// Unmarshal: 1 line only
Result r = JAXB.unmarshal(new StringReader(s), Result.class);
我将XML响应分配给字符串缓冲区。我将不得不从火车上解下行李stringbuffer@Karthi请参阅我从String
或StringBuffer
.Hi@icza编辑的解组答案。我在线程“main”java.lang.NullPointerException错误中得到异常,现在出现在以下行中:System.out.println(“\tListtype start:”+r.listtype.start);System.out.println(“\t列表类型结束:“+r.listtype.end”);System.out.println(“\tListtype总计:+r.listtype.total”)@Karthi这意味着您的Listtype
对象未创建。很可能您使用了不同的java代码或XML。如果您想在这方面获得更多帮助,可以通过编辑问题或作为新问题发布您使用的确切Java代码和XML。
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);
Result r = JAXB.unmarshal(new StringReader(sb.toString()), Result.class);