Java 解组-内部(子)元素为空
我收到了以下XML作为响应:Java 解组-内部(子)元素为空,java,jaxb,unmarshalling,Java,Jaxb,Unmarshalling,我收到了以下XML作为响应: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns="**********">
<SOAP-ENV:Body>
<ns1:RemitInterfaceAPIResponse
xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/">
<return xsi:type="tns:RemitInterfaceSoapObject">
<TransactionNumber xsi:type="xsd:string">12345</TransactionNumber>
<Status xsi:type="xsd:string">STRING</Status>
<StatusDesc xsi:type="xsd:string">String string string.</StatusDesc>
<Code xsi:type="xsd:string">STR</Code>
</return>
</ns1:RemitInterfaceAPIResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
汇款接口AppireResponseObject.java:
@XmlAccessorType(XmlAccessType.FIELD)
public class RemitInterfaceAPIResponseObject {
@XmlElement(name = "TransactionNumber")
private String transactionNumber;
@XmlElement(name = "Status")
private String status;
@XmlElement(name = "StatusDesc")
private String statusDesc;
@XmlElement(name = "Code")
private String code;
public String getTransactionNumber() { return transactionNumber; }
public void setTransactionNumber(String transactionNumber) { this.transactionNumber = transactionNumber; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getStatusDesc() { return statusDesc; }
public void setStatusDesc(String statusDesc) { this.statusDesc = statusDesc; }
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }
}
当我试图解组到reliedInterfaceApiResponse
的实例时,我得到了一个有效的reliedInterfaceApiResponse
对象实例,但soapObjects
为空
以下是我的解组代码:
SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(responseString.getBytes(StringConstants.UTF_8)));
JAXBContext jaxbContext = JAXBContext.newInstance(RemitInterfaceAPIResponse.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return (T) unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
非常感谢您的帮助。谢谢 我们可以看到的一件事是元素
interfaceeapiresponse
位于名称空间http://schemas.xmlsoap.org/soap/envelope/
(这很奇怪,因为这通常只用于SOAP信封元素),而元素return
没有名称空间。也许您的Java类将它们都放在了名称空间中?@Seelenvirtuose我想您可能是对的。我的包信息
说:@javax.xml.bind.annotation.XmlSchema(名称空间=”http://schemas.xmlsoap.org/soap/envelope/“,elementFormDefault=javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
。包含这两个类的包。有没有办法说“名称空间仅与根元素相关”?我对JAXB不太熟悉,但您可以尝试使用@xmlement(name=“return”,Namespace=”“)
。或者干脆将这两个类放在单独的包中。@Seelenvirtuose在@xmlement(name=“return”,namespace=“”)
:-(那么……对不起……请自己研究一下。你现在可能有线索了。
SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(responseString.getBytes(StringConstants.UTF_8)));
JAXBContext jaxbContext = JAXBContext.newInstance(RemitInterfaceAPIResponse.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return (T) unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());