2016-04-11 4 views
1

をXML文字列を変換することができません:私はXMLを持って適切にJAXBオブジェクトに

 <?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; 
    } 

} 

私はこのようなJAXBオブジェクトに変換しようとしています、私はxmlMessageと呼ばれる文字列の中の私の上記のXMLがあるとします。

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

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

しかしSystem.out.println(msghdr.getVersion());プリントnull。私が間違っているところを指摘できますか?

答えて

1

XML文書全体をMessageHeaderにデシリアライズしようとしていますが、<hdr:MessageHeader>子要素だけをデシリアライズしてMessageHeaderにデシリアライズする必要があります。

XML文書のルート要素<pfx:UpdateOrderPickedStatusRequest>には、<hdr:Version>要素がありません。また、MessageHeaderクラスの注釈と一致する名前の子要素もありません。 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サブ要素の子プロパティを持っています。

あなたは、その後の代わりに、MessageHeader対象に、UpdateOrderPickedStatusRequestオブジェクトにXML文書をデシリアライズするためにJAXBを使用します。

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

あなたはその後、JAXBはMessageHeader<hdr:MessageHeader>サブ要素の内容をデシリアライズすることを見つける必要がありますサブオブジェクトであるため、MessageHeaderオブジェクトのさまざまなプロパティが設定されます。上の行から取得したリクエストオブジェクトからヘッダーを取得し、ヘッダー内にバージョンを入れることができます。

+0

ありがとうございました! :)これはJAXBオブジェクトに関する私の考えを助けただけでなく、 –

関連する問題