gpt4 book ai didi

Java 解码 xml 元素

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:12 25 4
gpt4 key购买 nike

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

我需要获取元素中的开始、计数、总计。我为此有两个类文件列表类型.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);

如何获取main函数中start、end、total的元素值

最佳答案

您必须创建一个 Java 类来建模 <result> XML 标记,也是一个建模 <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 存在于 StringBuffer 中, StringBuilder或作为 String ,您可以通过创建 StringReader 来解码它作为源而不是 File :

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

如果你有它在 StringBufferStringBuilder命名sb ,您可以将其转换为String使用其 toString()方法:

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

关于Java 解码 xml 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28452937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com