XML到pojojava
我读过其他与这个问题相关的帖子,但我无法解决我的问题。我尝试将以下XML字符串转换为JAVA类,但当我尝试使用XML到pojojava,java,xml,Java,Xml,我读过其他与这个问题相关的帖子,但我无法解决我的问题。我尝试将以下XML字符串转换为JAVA类,但当我尝试使用getParam1()方法访问param1时,它返回null,我不确定原因 XML字符串: <?xml version="1.0" encoding="utf-8"?> <REQUERYTRXRESPONSE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.o
getParam1()
方法访问param1时,它返回null,我不确定原因
XML字符串:
<?xml version="1.0" encoding="utf-8"?>
<REQUERYTRXRESPONSE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<param1>3gbahtJf1y85Oks4HrPLkqTQZV8Yg8pIhdXOrZ8pLGJP3FLwqKlIzIl/GgUpGvFaw4MC4SV+4pCudmVq+apIMIJJS4PrVyUx4T0ZO/Tsui4ZqCn62dLAG0DVhBVz2ZasF4yr7CRYnk47FWS0RywXmA==</param1>
<param2>lO4ismiJwsvBiHQGW/UwCA==</param2>
<param3 />
</REQUERYTRXRESPONSE>
XML到Java类代码:
HttpRequest httpRequest = HttpRequest.get();
if (httpRequest.ok()) {
String response = httpRequest.body();
System.out.println(response);
JAXBContext jaxbContext = JAXBContext.newInstance(REQUERYTRXRESPONSE.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
REQUERYTRXRESPONSE requerytrxresponse = (REQUERYTRXRESPONSE) unmarshaller.unmarshal(new StringReader(response));
System.out.println((String) requerytrxresponse.getParam1()); // returns null
}
我设法弄明白了
@XmlRootElement(name = "REQUERYTRXRESPONSE")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
private String param1;
private String param2;
private String param3;
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
public String getParam2() {
return param2;
}
public void setParam2(String param2) {
this.param2 = param2;
}
public String getParam3() {
return param3;
}
public void setParam3(String param3) {
this.param3 = param3;
}
}
执行@XxmlAccessorType
时,不需要指定@xmlement
,除非需要required=true
部分
我改变的是,我将命名空间从包info.java
类中的@XmlRootElement
移动,如下所示:
@javax.xml.bind.annotation.XmlSchema(namespace = "http://tempuri.org/",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.sfatandrei.soplayground.model;
我的主要测试方法包括:
final InputStream resourceAsStream = SoPlaygroundApplication.class.getClassLoader().getResourceAsStream("test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Response response = (Response) unmarshaller.unmarshal(resourceAsStream);
System.out.println(response);
对我来说,它工作得很好。确保编码正确,并检查jaxb提供程序。我使用默认的sun实现com.sun.xml.bind.v2.runtime.JAXBContextImpl对其进行了测试 对解组代码进行测试:
@Test
public void testUnmarshaller() throws JAXBException, IOException {
final InputStream expectedXmlResource = getClass().getResourceAsStream("/REQUERYTRXRESPONSE.xml");
StringWriter stringWriter = new StringWriter();
IOUtils.copy(expectedXmlResource, stringWriter, "UTF-8");
JAXBContext jaxbContext = JAXBContext.newInstance(REQUERYTRXRESPONSE .class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
REQUERYTRXRESPONSE requerytrxresponse = (REQUERYTRXRESPONSE) unmarshaller.unmarshal(new StringReader(stringWriter.toString()));
assertEquals(requerytrxresponse.getParam1(), "3gbahtJf1y85Oks4HrPLkqTQZV8Yg8pIhdXOrZ8pLGJP3FLwqKlIzIl/GgUpGvFaw4MC4SV+4pCudmVq+apIMIJJS4PrVyUx4T0ZO/Tsui4ZqCn62dLAG0DVhBVz2ZasF4yr7CRYnk47FWS0RywXmA==");
}
您能否将响应打印到控制台,以检查它是否具有正确的XML内容?这将是我要检查的第一件事。我想你需要为setters添加注释。@shmosel我不认为so@TimBiegeleisen上面的xml字符串是将响应打印到控制台的结果。我从控制台复制它。只是一个提示:将来,简单地从上下文创建封送拆收器,从类实例化一个对象,将其封送到一个文件中。通过这种方式,您可以查看类定义的XML,然后处理它们之间的差异。实际上,您不需要任何注释,除非您想要非默认行为。或者,您也可以简单地将名称空间声明复制到元素中。问题是,您的类没有反映xml:类说“元素没有名称空间”,但xml定义了
http://tempuri.org/
。@FlorianSchaetz,你是对的,但是从可读性和可维护性的角度来看,包-info.java是一种更简单的方法。@FlorianSchaetz谢谢,这很有帮助
@Test
public void testUnmarshaller() throws JAXBException, IOException {
final InputStream expectedXmlResource = getClass().getResourceAsStream("/REQUERYTRXRESPONSE.xml");
StringWriter stringWriter = new StringWriter();
IOUtils.copy(expectedXmlResource, stringWriter, "UTF-8");
JAXBContext jaxbContext = JAXBContext.newInstance(REQUERYTRXRESPONSE .class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
REQUERYTRXRESPONSE requerytrxresponse = (REQUERYTRXRESPONSE) unmarshaller.unmarshal(new StringReader(stringWriter.toString()));
assertEquals(requerytrxresponse.getParam1(), "3gbahtJf1y85Oks4HrPLkqTQZV8Yg8pIhdXOrZ8pLGJP3FLwqKlIzIl/GgUpGvFaw4MC4SV+4pCudmVq+apIMIJJS4PrVyUx4T0ZO/Tsui4ZqCn62dLAG0DVhBVz2ZasF4yr7CRYnk47FWS0RywXmA==");
}