我已阅读与此问题相关的其他帖子但我无法解决我的问题。我尝试将以下XML String转换为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.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<param1>3gbahtJf1y85Oks4HrPLkqTQZV8Yg8pIhdXOrZ8pLGJP3FLwqKlIzIl/GgUpGvFaw4MC4SV+4pCudmVq+apIMIJJS4PrVyUx4T0ZO/Tsui4ZqCn62dLAG0DVhBVz2ZasF4yr7CRYnk47FWS0RywXmA==</param1>
<param2>lO4ismiJwsvBiHQGW/UwCA==</param2>
<param3 />
</REQUERYTRXRESPONSE>
Java类:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(namespace = "http://tempuri.org/", name = "REQUERYTRXRESPONSE")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class REQUERYTRXRESPONSE {
private String param1;
private String param2;
private String param3;
@XmlElement(required = true, name = "param1")
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
@XmlElement(required = true, name = "param2")
public String getParam2() {
return param2;
}
public void setParam2(String param2) {
this.param2 = param2;
}
@XmlElement(required = true, name = "param3")
public String getParam3() {
return param3;
}
public void setParam3(String param3) {
this.param3 = param3;
}
}
XML to 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
}
答案 0 :(得分:7)
管理好了解。
In [142]: Table1 = FamilyVsWWTotal.copy()
In [143]: Table1['Region'] = 'K'
In [144]: Table1
Out[144]:
Country date Region
0 USA 2011-4-3 K
1 CAN 2011-3-5 K
2 USA 2011-4-3 K
3 MEX 2011-5-7 K
In [145]: FamilyVsWWTotal
Out[145]:
Country date
0 USA 2011-4-3
1 CAN 2011-3-5
2 USA 2011-4-3
3 MEX 2011-5-7
}
除非您需要@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;
}
部分,否则在执行@XmlElement
时无需指定@XxmlAccessorType
。
我更改的是,我将required=true
类中的名称空间移动到@XmlRootElement
类中,如下所示:
package-info.java
我的主要测试方法包括:
@javax.xml.bind.annotation.XmlSchema(namespace = "http://tempuri.org/",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.sfatandrei.soplayground.model;
答案 1 :(得分:1)
对我而言,它运作得很好。确保您有正确的编码并检查您的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==");
}