1

I have my 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;
    }

}

Suppose I have my above XML in a string called xmlMessage, I am trying to convert it into JAXB object like this :

StringReader xmlReader = new StringReader(xmlMessage);
            MessageHeader msghdr = JAXB.unmarshal(xmlReader, MessageHeader.class);

            System.out.println(msghdr.getVersion());

But the System.out.println(msghdr.getVersion()); prints null. Can you please point out where I am going wrong?

1 Answer 1

1

You are trying to deserialize the whole XML document into a MessageHeader, when what you want to do is to deserialize just the <hdr:MessageHeader> child element into a MessageHeader.

The root element of your XML document, <pfx:UpdateOrderPickedStatusRequest>, has no <hdr:Version> element, nor does it have any child elements whose names match annotations in your MessageHeader class. (It just has two child elements, MessageHeader and MessageBody.) JAXB tries to match up child elements of <pfx:UpdateOrderPickedStatusRequest> with properties of your MessageHeader object, but JAXB isn't able to match anything. As a result, all the properties of your MessageHeader remain with their default value of null.

What you need to do instead is to create a class that represents the root element of the XML document, for example:

public class UpdateOrderPickedStatusRequest {

    private MessageHeader header;

    @XmlElement(name = "MessageHeader")
    public MessageHeader getHeader() {
        return header;
    }

    public void setHeader(MessageHeader header) {
        this.header = header;
    }
}

This class has a child property for the MessageHeader sub-element.

You then use JAXB to deserialize your XML document into an UpdateOrderPickedStatusRequest object, instead of into a MessageHeader object:

    UpdateOrderPickedStatusRequest request = JAXB.unmarshal(xmlReader, UpdateOrderPickedStatusRequest.class);

You should then find that JAXB deserializes the contents of the <hdr:MessageHeader> sub-element into a MessageHeader sub-object, and so the various properties in the MessageHeader object should get set. You can then grab the header from the request object you get from the line above, and within the header should be the version.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! :) This not only helped but also cleared my concepts regarding JAXB objects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.