我有我的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>
子元素反序列化为MessageHeader
。
XML文档的根元素<pfx:UpdateOrderPickedStatusRequest>
没有<hdr:Version>
元素,也没有任何子元素的名称与MessageHeader
类中的注释匹配。 (它只有两个子元素MessageHeader
和MessageBody
。)JAXB尝试将<pfx:UpdateOrderPickedStatusRequest>
的子元素与MessageHeader
对象的属性进行匹配,但是JAXB无法匹配任何内容。因此,MessageHeader
的所有属性均保留其默认值null
。
相反,您需要做的是创建一个表示XML文档根元素的类,例如:
public class UpdateOrderPickedStatusRequest {
private MessageHeader header;
@XmlElement(name = "MessageHeader")
public MessageHeader getHeader() {
return header;
}
public void setHeader(MessageHeader header) {
this.header = header;
}
}
此类具有
MessageHeader
子元素的子属性。然后,您可以使用JAXB将XML文档反序列化为
UpdateOrderPickedStatusRequest
对象,而不是反序列化为MessageHeader
对象: UpdateOrderPickedStatusRequest request = JAXB.unmarshal(xmlReader, UpdateOrderPickedStatusRequest.class);
然后,您应该发现JAXB将
<hdr:MessageHeader>
子元素的内容反序列化为MessageHeader
子对象,因此应设置MessageHeader
对象中的各种属性。然后,您可以从上一行获得的请求对象中获取标头,标头内应为版本。