无法正确地将XML String转换为JAXB对象

时间:2016-04-11 17:15:36

标签: java xml xsd jaxb

我有我的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。你能指出我哪里出错吗?

1 个答案:

答案 0 :(得分:1)

您尝试将整个XML文档反序列化为MessageHeader,当您要执行的操作是将<hdr:MessageHeader>子元素反序列化为MessageHeader时。

XML文档的根元素<pfx:UpdateOrderPickedStatusRequest>没有<hdr:Version>元素,也没有任何名称与MessageHeader类中的注释匹配的子元素。 (它只有两个子元素MessageHeaderMessageBody。)JAXB尝试将<pfx:UpdateOrderPickedStatusRequest>的子元素与MessageHeader对象的属性进行匹配,但JAXB不是&#39能够匹配任何东西。因此,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对象中的各种属性。然后,您可以从上面的行中获取请求对象中的标题,并且标题内应该是版本。