gpt4 book ai didi

java - 如何解析从客户端收到的xml结果

转载 作者:行者123 更新时间:2023-12-01 13:26:20 25 4
gpt4 key购买 nike

以下代码向 Web 服务发送请求并接收结果。它成功发送请求并在控制台上显示结果,问题是我无法将 xml 解析为对象。

代码

    Request req = new Request();
req.setLanguage(lang);

webser webs = new webser();
webs.setRequest(req);

JAXBContext context = JAXBContext.newInstance(webser.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");

OutputStream os = connection.getOutputStream();
m.marshal(webs, os);

WebResponse result = (WebResponse) JAXB.unmarshal(connection.getInputStream(), WebResponse.class);
System.err.println(">>>>>>" + result.getResponse().getInfogetPersons().get(0).getId());
//does not return anything

//**** populating the object manually ****
String edd = ">";
person p1 = new Person();
p1.setId(1);
Person p2 = new Person();
p2.setId(2);

Info info = new linfo();
info.getPersons().add(p1);
info.gePersons().add(p2);

Response r = new Response();
r.setinfo(info);

WebResponse result2 = new WebResponse();
result2.setResponse(r);

JAXB.marshal(result2, edd);
System.err.println("Result:" + edd); << does not return anything

connection.disconnect();

} catch (IOException e) {
return e.getMessage();
} catch (JAXBException e) {
return e.getMessage();
}

WebResponse.java

@XmlRootElement
public class WebResponse {

@XmlElement(name = "Response")
private Response response = null;

public Response getResponse() {
return response;
}

public void setResponse(Response response) {
this.response = response;
}
}

Response.java

@XmlRootElement
public class Response {
@XmlRootElement (name = "Info")
private Info info;
...
}

Info.java

   @XmlRootElement
public class Info {
@XmlRootElement ( name = "Person")
private List<Person> person;
....
}

Person.java

   @XmlRootElement
public class Person{

private int Id;

@XmlElement (name = "Id")
public int getId() {
return Id;
}

public void setId(int Id) {
this.Id = Id;
}
}

服务器的实际响应

<?xml version="1.0" encoding="UTF-8"?>
<WebResponse>
<Response target="test">
<Info>
<Person>
<Id>83094</Id>
<Age>34</Age>
<FGs>
<HF>0</HF>
<HI>1</HI>
</FGs>
<Rt>
<Rid parentid="412404">201735813</Rid>
<Rname>Special</Rname>
<Ca>2</Ca>
<link>www.google.com/test</link>
</Rt>
<Rt>
<Rid parentid="777777">787878</Rid>
<Rname>Standard</Rname>
<Ca>7</Ca>
<link>www.yahoo.com/blank</link>
</Rt>
</Person>
<Person>
....
</Person>
......
</Info>
.....

最佳答案

您无法直接解码到 List<Response> 。您将需要解码具有 List<Response> 类型的映射属性的类。相反。

更新#1

此外,您要解码的类需要与您要解码的 XML 级别相对应。按照您的代码现在的情况,您需要解码到表示 Web 响应的类。如果您想解码文档中的子部分,您可以开始使用 StAX 解析 XML,然后前进 XMLStreamReader到适当的位置并解码它。

更新#2

由于您有点困难,下面是您可以使用的模型。

Java 模型

网络响应

如果您要注释字段(实例变量),那么您应该指定 @XmlAccessorType(XmlAccessType.FIELD)在您的类(class)上(请参阅:http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html)。

import javax.xml.bind.annotation.*;

@XmlRootElement(name="WebResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class WebResponse {

@XmlElement(name = "Response")
private Response response = null;

}

回应

您可以消除 Info使用 @XmlElementWrapper 进行类用于表示分组元素的注释(请参阅: http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html )。

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Response {

@XmlElementWrapper(name = "Info")
@XmlElement(name = "Person")
private List<Person> person;

@XmlAttribute
private String target;

}

您只需使用 @XmlRootElement在您想要编码的顶级类(class)上。

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Person {

@XmlElement(name = "Id")
private int id;

}

演示代码

演示

import java.io.File;
import javax.xml.bind.*;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(WebResponse.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum21797000/input.xml");
WebResponse wr = (WebResponse) unmarshaller.unmarshal(xml);

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(wr, System.out);
}

}

输入.xml/输出

<?xml version="1.0" encoding="UTF-8"?>
<WebResponse>
<Response target="test">
<Info>
<Person>
<Id>83094</Id>
</Person>
<Person>
<Id>0</Id>
</Person>
</Info>
</Response>
</WebResponse>

关于java - 如何解析从客户端收到的xml结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21797000/

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