我使用了Web服务,并且正在接收具有相同名称的父节点和子节点的XML响应。问题在于最后一个层次结构没有值。
从我的角度来看,JAXB应该处理一个列表TestDetails。
班级信封:
@XmlRootElement(name="Envelope", namespace="http://schemas.xmlsoap.org/soap/envelope/")
@XmlAccessorType(XmlAccessType.FIELD)
public class Envelope {
@XmlElement(name="Body", namespace="http://schemas.xmlsoap.org/soap/envelope/")
private Body Body;
}
类主体:
@XmlAccessorType(XmlAccessType.FIELD)
public class Body {
@XmlElement(name="GetTestlistWithConnectionsResponse", namespace="http://tempuri.org/")
private GetTestlistWithConnectionsResponse GetTestlistWithConnectionsResponse;
public Body() {}
}
GetTestlistWithConnectionsResponse类:
@XmlAccessorType(XmlAccessType.FIELD)
public class GetTestlistWithConnectionsResponse {
public GetTestlistWithConnectionsResponse() {}
@XmlElement(name="GetTestlistWithConnectionsResult",
namespace="http://tempuri.org/")
private GetTestlistWithConnectionsResult GetTestlistWithConnectionsResult;
}
GetTestlistWithConnectionsResult类:
@XmlAccessorType(XmlAccessType.FIELD)
public class GetTestlistWithConnectionsResult {
public GetTestlistWithConnectionsResult() {}
@XmlElement(name="TestDetails", namespace="http://schemas.datacontract.org/XXX")
private TestDetails TestDetails ;
}
Class TestDetails:
@XmlAccessorType(XmlAccessType.FIELD)
public class TestDetails{
public TestDetails() {}
@XmlElement(name="A", namespace="http://schemas.datacontract.org/XXX")
private String A;
@XmlElement(name="B", namespace="http://schemas.datacontract.org/XXX")
private String B;
@XmlElement(name="TestDetails")
private List<TestDetails> TestDetails = new ArrayList<TestDetails>();
}
XML结构:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetTestlistWithConnectionsResponse xmlns="http://tempuri.org/">
<GetTestlistWithConnectionsResult xmlns:a="http://schemas.datacontract.org/XXX" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Error i:nil="true"/>
<a:TestDetails>
<a:TestDetails>
<a:A>A</a:A>
<a:B>B</a:B>
</a:TestDetails>
</a:TestDetails>
</GetTestlistWithConnectionsResult>
</GetTestlistWithConnectionsResponse>
</s:Body>
</s:Envelope>
解组方法:
public Envelope unmarshallFromFile(){
Envelope testDetail= null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Envelope.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
InputStream inStream = null;
try {
inStream = new FileInputStream(this.fileLoc);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
flightDetailsSN = (Envelope) jaxbUnmarshaller.unmarshal( inStream );
} catch (JAXBException e) {
e.printStackTrace();
}
return testDetail;
}
当我调用unmarshall方法时,会收到一个对象,该对象带有带有空列表的TestDetails项。我期望列表包含一个值为A和B的元素。
答案 0 :(得分:0)
尝试此操作,如果没有问题,请更改子元素或子名称。 (我认为这是因为标题和子元素的名称相同。)
<a:TestDetails>
<a:TestDetail>
<a:A>A</a:A>
<a:B>B</a:B>
</a:TestDetail>
</a:TestDetails>
答案 1 :(得分:0)
var payoutModel = new payout_transaction
{
senderRefId = obj.senderRefId,
requestDate = obj.tranRequestDate,
.... other properties
}
SaveTransaction(payoutModel);
和A
是元素,而不是属性。尝试更改
B
到
@XmlAttribute
private String A;
private String B;