无法将 XML 字符串正确转换为 JAXB 对象
Unable to convert XML String to JAXB object properly
我有我的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<pfx:UpdateOrderPickedStatusRequest xmlns:flt="http://www.example.com"
xmlns:hdr="http://www.example2.com/Header/datatypes/MessageHeader/1.4/"
xmlns:pfx="http://www.example3.com/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.xmlns.djd.com/new/final.xsd ">
<hdr:MessageHeader>
<hdr:SubId>ABC</hdr:SubId>
<hdr:CnsmrId>PQR</hdr:CnsmrId>
<hdr:SrvcNm>QWERTY</hdr:SrvcNm>
<hdr:TranId>1234</hdr:TranId>
<hdr:Version>2.7</hdr:Version>
</hdr:MessageHeader>
<pfx:MessageBody>
<pfx:customerOrder>
<pfx:orderHeader>
<pfx:orderNumber>6666111999</pfx:orderNumber>
</pfx:orderHeader>
</pfx:customerOrder>
</pfx:MessageBody>
</pfx:UpdateOrderPickedStatusRequest>
Update.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xml="http://www.w3.org/XML/1998/namespace"
elementFormDefault="qualified" attributeFormDefault="qualified"
targetNamespace="http://www.example44.com"
xmlns:pfx="http://www.example3.com/"
version="2.7" xmlns:hdr="http://www.example2.com/Header/datatypes/MessageHeader/1.4/"
xmlns:flt="http://www.example.com">
<xsd:import namespace="http://www.example2.com/Header/datatypes/MessageHeader/1.4/"
schemaLocation="MessageHeader.xsd"/>
<xsd:complexType name="CustomerOrder">
<xsd:sequence>
<xsd:element name="orderHeader" type="pfx:OrderHeader"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
MessageHeader.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xml="http://www.w3.org/XML/1998/namespace"
elementFormDefault="qualified" attributeFormDefault="qualified"
targetNamespace="http://www.example44.com"
xmlns:pfx="http://www.example3.com/"
version="2.7" xmlns:hdr="http://www.example2.com/Header/datatypes/MessageHeader/1.4/"
xmlns:flt="http://www.example.com">
<xsd:import namespace="http://www.example44.com/"
schemaLocation="Update.xsd"/>
<xsd:simpleType name="alpha255">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="255"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="MessageHeader" type="hdr:MessageHeader"/>
<xsd:complexType name="MessageHeader">
<xsd:sequence>
<xsd:element name="SubId" type="pfx:alpha255"/>
<xsd:element name="CnsmrId" type="pfx:alpha255"/>
<xsd:element name="SrvcNm" type="pfx:alpha255"/>
<xsd:element name="AppId" type="pfx:alpha255" minOccurs="0"/>
<xsd:element name="TranId" type="pfx:alpha255" minOccurs="0"/>
<xsd:element name="UsrNm" type="pfx:alpha255" minOccurs="0"/>
<xsd:element name="Pwd" type="pfx:alpha255" minOccurs="0"/>
<xsd:element name="SrcSysId" type="pfx:alpha255" minOccurs="0"/>
<xsd:element name="Version" type="pfx:alpha255"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
MessageHeader.java :
public class MessageHeader {
@XmlElement(name = "SubId", required = true)
protected String subId;
@XmlElement(name = "CnsmrId", required = true)
protected String cnsmrId;
@XmlElement(name = "SrvcNm", required = true)
protected String srvcNm;
@XmlElement(name = "AppId")
protected String appId;
@XmlElement(name = "TranId")
protected String tranId;
@XmlElement(name = "UsrNm")
protected String usrNm;
@XmlElement(name = "Pwd")
protected String pwd;
@XmlElement(name = "SrcSysId")
protected String srcSysId;
@XmlElement(name = "Version", required = true)
protected String version;
@XmlElement(name = "ConfCd")
protected String confCd;
/**
* Gets the value of the subId property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getSubId() {
return subId;
}
/**
* Sets the value of the subId property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setSubId(String value) {
this.subId = value;
}
/**
* Gets the value of the cnsmrId property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getCnsmrId() {
return cnsmrId;
}
/**
* Sets the value of the cnsmrId property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setCnsmrId(String value) {
this.cnsmrId = value;
}
/**
* Gets the value of the srvcNm property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getSrvcNm() {
return srvcNm;
}
/**
* Sets the value of the srvcNm property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setSrvcNm(String value) {
this.srvcNm = value;
}
/**
* Gets the value of the appId property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getAppId() {
return appId;
}
/**
* Sets the value of the appId property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setAppId(String value) {
this.appId = value;
}
/**
* Gets the value of the tranId property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getTranId() {
return tranId;
}
/**
* Sets the value of the tranId property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setTranId(String value) {
this.tranId = value;
}
/**
* Gets the value of the usrNm property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getUsrNm() {
return usrNm;
}
/**
* Sets the value of the usrNm property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUsrNm(String value) {
this.usrNm = value;
}
/**
* Gets the value of the pwd property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getPwd() {
return pwd;
}
/**
* Sets the value of the pwd property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setPwd(String value) {
this.pwd = value;
}
/**
* Gets the value of the srcSysId property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getSrcSysId() {
return srcSysId;
}
/**
* Sets the value of the srcSysId property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setSrcSysId(String value) {
this.srcSysId = value;
}
/**
* Gets the value of the version property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getVersion() {
return version;
}
/**
* Sets the value of the version property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setVersion(String value) {
this.version = value;
}
/**
* Gets the value of the confCd property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getConfCd() {
return confCd;
}
/**
* Sets the value of the confCd property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setConfCd(String value) {
this.confCd = value;
}
}
假设我在名为 xmlMessage 的字符串中有上面的 XML,我正在尝试将其转换为 JAXB 对象,如下所示:
StringReader xmlReader = new StringReader(xmlMessage);
MessageHeader msghdr = JAXB.unmarshal(xmlReader, MessageHeader.class);
System.out.println(msghdr.getVersion());
但是 System.out.println(msghdr.getVersion());
打印 null
。你能指出我哪里错了吗?
您正在尝试将整个 XML 文档反序列化为 MessageHeader
,而您要做的只是将 <hdr:MessageHeader>
child 元素反序列化为 MessageHeader
MessageHeader
.
您的 XML 文档的根元素 <pfx:UpdateOrderPickedStatusRequest>
没有 <hdr:Version>
元素,也没有任何 child 名称与您的 MessageHeader
class。 (它只有两个 child 元素,MessageHeader
和 MessageBody
。)JAXB 尝试将 <pfx:UpdateOrderPickedStatusRequest>
的 child 元素与您的 MessageHeader
object,但 JAXB 无法匹配任何内容。因此,您的 MessageHeader
的所有属性都保持默认值 null
。
您需要做的是创建一个 class 代表 XML 文档的根元素,例如:
public class UpdateOrderPickedStatusRequest {
private MessageHeader header;
@XmlElement(name = "MessageHeader")
public MessageHeader getHeader() {
return header;
}
public void setHeader(MessageHeader header) {
this.header = header;
}
}
此 class 有一个 child 属性 用于 MessageHeader
sub-element。
然后使用 JAXB 将 XML 文档反序列化为 UpdateOrderPickedStatusRequest
object,而不是 MessageHeader
object:
UpdateOrderPickedStatusRequest request = JAXB.unmarshal(xmlReader, UpdateOrderPickedStatusRequest.class);
然后您应该会发现 JAXB 将 <hdr:MessageHeader>
sub-element 的内容反序列化为 MessageHeader
sub-object,因此 MessageHeader
中的各种属性] object 应该设置好。然后,您可以从上面一行获得的请求 object 中获取 header,在 header 中应该是版本。
我有我的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<pfx:UpdateOrderPickedStatusRequest xmlns:flt="http://www.example.com"
xmlns:hdr="http://www.example2.com/Header/datatypes/MessageHeader/1.4/"
xmlns:pfx="http://www.example3.com/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.xmlns.djd.com/new/final.xsd ">
<hdr:MessageHeader>
<hdr:SubId>ABC</hdr:SubId>
<hdr:CnsmrId>PQR</hdr:CnsmrId>
<hdr:SrvcNm>QWERTY</hdr:SrvcNm>
<hdr:TranId>1234</hdr:TranId>
<hdr:Version>2.7</hdr:Version>
</hdr:MessageHeader>
<pfx:MessageBody>
<pfx:customerOrder>
<pfx:orderHeader>
<pfx:orderNumber>6666111999</pfx:orderNumber>
</pfx:orderHeader>
</pfx:customerOrder>
</pfx:MessageBody>
</pfx:UpdateOrderPickedStatusRequest>
Update.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xml="http://www.w3.org/XML/1998/namespace"
elementFormDefault="qualified" attributeFormDefault="qualified"
targetNamespace="http://www.example44.com"
xmlns:pfx="http://www.example3.com/"
version="2.7" xmlns:hdr="http://www.example2.com/Header/datatypes/MessageHeader/1.4/"
xmlns:flt="http://www.example.com">
<xsd:import namespace="http://www.example2.com/Header/datatypes/MessageHeader/1.4/"
schemaLocation="MessageHeader.xsd"/>
<xsd:complexType name="CustomerOrder">
<xsd:sequence>
<xsd:element name="orderHeader" type="pfx:OrderHeader"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
MessageHeader.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xml="http://www.w3.org/XML/1998/namespace"
elementFormDefault="qualified" attributeFormDefault="qualified"
targetNamespace="http://www.example44.com"
xmlns:pfx="http://www.example3.com/"
version="2.7" xmlns:hdr="http://www.example2.com/Header/datatypes/MessageHeader/1.4/"
xmlns:flt="http://www.example.com">
<xsd:import namespace="http://www.example44.com/"
schemaLocation="Update.xsd"/>
<xsd:simpleType name="alpha255">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="255"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="MessageHeader" type="hdr:MessageHeader"/>
<xsd:complexType name="MessageHeader">
<xsd:sequence>
<xsd:element name="SubId" type="pfx:alpha255"/>
<xsd:element name="CnsmrId" type="pfx:alpha255"/>
<xsd:element name="SrvcNm" type="pfx:alpha255"/>
<xsd:element name="AppId" type="pfx:alpha255" minOccurs="0"/>
<xsd:element name="TranId" type="pfx:alpha255" minOccurs="0"/>
<xsd:element name="UsrNm" type="pfx:alpha255" minOccurs="0"/>
<xsd:element name="Pwd" type="pfx:alpha255" minOccurs="0"/>
<xsd:element name="SrcSysId" type="pfx:alpha255" minOccurs="0"/>
<xsd:element name="Version" type="pfx:alpha255"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
MessageHeader.java :
public class MessageHeader {
@XmlElement(name = "SubId", required = true)
protected String subId;
@XmlElement(name = "CnsmrId", required = true)
protected String cnsmrId;
@XmlElement(name = "SrvcNm", required = true)
protected String srvcNm;
@XmlElement(name = "AppId")
protected String appId;
@XmlElement(name = "TranId")
protected String tranId;
@XmlElement(name = "UsrNm")
protected String usrNm;
@XmlElement(name = "Pwd")
protected String pwd;
@XmlElement(name = "SrcSysId")
protected String srcSysId;
@XmlElement(name = "Version", required = true)
protected String version;
@XmlElement(name = "ConfCd")
protected String confCd;
/**
* Gets the value of the subId property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getSubId() {
return subId;
}
/**
* Sets the value of the subId property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setSubId(String value) {
this.subId = value;
}
/**
* Gets the value of the cnsmrId property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getCnsmrId() {
return cnsmrId;
}
/**
* Sets the value of the cnsmrId property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setCnsmrId(String value) {
this.cnsmrId = value;
}
/**
* Gets the value of the srvcNm property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getSrvcNm() {
return srvcNm;
}
/**
* Sets the value of the srvcNm property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setSrvcNm(String value) {
this.srvcNm = value;
}
/**
* Gets the value of the appId property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getAppId() {
return appId;
}
/**
* Sets the value of the appId property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setAppId(String value) {
this.appId = value;
}
/**
* Gets the value of the tranId property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getTranId() {
return tranId;
}
/**
* Sets the value of the tranId property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setTranId(String value) {
this.tranId = value;
}
/**
* Gets the value of the usrNm property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getUsrNm() {
return usrNm;
}
/**
* Sets the value of the usrNm property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUsrNm(String value) {
this.usrNm = value;
}
/**
* Gets the value of the pwd property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getPwd() {
return pwd;
}
/**
* Sets the value of the pwd property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setPwd(String value) {
this.pwd = value;
}
/**
* Gets the value of the srcSysId property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getSrcSysId() {
return srcSysId;
}
/**
* Sets the value of the srcSysId property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setSrcSysId(String value) {
this.srcSysId = value;
}
/**
* Gets the value of the version property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getVersion() {
return version;
}
/**
* Sets the value of the version property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setVersion(String value) {
this.version = value;
}
/**
* Gets the value of the confCd property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getConfCd() {
return confCd;
}
/**
* Sets the value of the confCd property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setConfCd(String value) {
this.confCd = value;
}
}
假设我在名为 xmlMessage 的字符串中有上面的 XML,我正在尝试将其转换为 JAXB 对象,如下所示:
StringReader xmlReader = new StringReader(xmlMessage);
MessageHeader msghdr = JAXB.unmarshal(xmlReader, MessageHeader.class);
System.out.println(msghdr.getVersion());
但是 System.out.println(msghdr.getVersion());
打印 null
。你能指出我哪里错了吗?
您正在尝试将整个 XML 文档反序列化为 MessageHeader
,而您要做的只是将 <hdr:MessageHeader>
child 元素反序列化为 MessageHeader
MessageHeader
.
您的 XML 文档的根元素 <pfx:UpdateOrderPickedStatusRequest>
没有 <hdr:Version>
元素,也没有任何 child 名称与您的 MessageHeader
class。 (它只有两个 child 元素,MessageHeader
和 MessageBody
。)JAXB 尝试将 <pfx:UpdateOrderPickedStatusRequest>
的 child 元素与您的 MessageHeader
object,但 JAXB 无法匹配任何内容。因此,您的 MessageHeader
的所有属性都保持默认值 null
。
您需要做的是创建一个 class 代表 XML 文档的根元素,例如:
public class UpdateOrderPickedStatusRequest {
private MessageHeader header;
@XmlElement(name = "MessageHeader")
public MessageHeader getHeader() {
return header;
}
public void setHeader(MessageHeader header) {
this.header = header;
}
}
此 class 有一个 child 属性 用于 MessageHeader
sub-element。
然后使用 JAXB 将 XML 文档反序列化为 UpdateOrderPickedStatusRequest
object,而不是 MessageHeader
object:
UpdateOrderPickedStatusRequest request = JAXB.unmarshal(xmlReader, UpdateOrderPickedStatusRequest.class);
然后您应该会发现 JAXB 将 <hdr:MessageHeader>
sub-element 的内容反序列化为 MessageHeader
sub-object,因此 MessageHeader
中的各种属性] object 应该设置好。然后,您可以从上面一行获得的请求 object 中获取 header,在 header 中应该是版本。