JAXB Unmarshal为嵌套对象

时间:2017-06-13 19:13:07

标签: java xml jaxb

我在尝试使用JAXB的unmarshal时遇到了一些麻烦。我从Web服务中收到以下XML:

<retEnviCte xmlns="http://www.portalfiscal.inf.br/cte" versao="3.00"><tpAmb>2</tpAmb><cUF>43</cUF><verAplic>RS20170524123312</verAplic><cStat>103</cStat><xMotivo>Lote recebido com sucesso</xMotivo><infRec><nRec>431000011194855</nRec><dhRecbto>2017-06-13T13:27:22-03:00</dhRecbto><tMed>1</tMed></infRec></retEnviCte>

我想将它解组为TRetEnviCTe类,如下所示:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TRetEnviCTe", propOrder = {
    "tpAmb",
    "cuf",
    "verAplic",
    "cStat",
    "xMotivo",
    "infRec"
})
public class TRetEnviCTe {

    @XmlElement(required = true)
    protected String tpAmb;
    @XmlElement(name = "cUF", required = true)
    protected String cuf;
    @XmlElement(required = true)
    protected String verAplic;
    @XmlElement(required = true)
    protected String cStat;
    @XmlElement(required = true)
    protected String xMotivo;
    protected TRetEnviCTe.InfRec infRec;
    @XmlAttribute(name = "versao", required = true)
    protected String versao;

//Getters and setters

@XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "nRec",
        "dhRecbto",
        "tMed"
    })
    public static class InfRec {

        @XmlElement(required = true)
        protected String nRec;
        @XmlElement(required = true)
        protected String dhRecbto;
        @XmlElement(required = true)
        protected BigInteger tMed;

        /**
     * Gets the value of the nRec property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getNRec() {
        return nRec;
    }

    /**
     * Sets the value of the nRec property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setNRec(String value) {
        this.nRec = value;
    }

    /**
     * Gets the value of the dhRecbto property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getDhRecbto() {
        return dhRecbto;
    }

    /**
     * Sets the value of the dhRecbto property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setDhRecbto(String value) {
        this.dhRecbto = value;
    }

    /**
     * Gets the value of the tMed property.
     * 
     * @return
     *     possible object is
     *     {@link BigInteger }
     *     
     */
    public BigInteger getTMed() {
        return tMed;
    }

    /**
     * Sets the value of the tMed property.
     * 
     * @param value
     *     allowed object is
     *     {@link BigInteger }
     *     
     */
    public void setTMed(BigInteger value) {
        this.tMed = value;
    }

  }
}

当我通过下面的方法执行unmarshal时,字段被正确填充,除了创建的InfRec对象,但其属性中包含空值。

private static TRetEnviCTe getDocumentRet(String xml) throws JAXBException {
        JAXBContext context2 = JAXBContext.newInstance(TRetEnviCTe.class);
        Unmarshaller unmarshaller2 = context2.createUnmarshaller();
        return unmarshaller2.unmarshal(new StreamSource(new StringReader(xml)), TRetEnviCTe.class).getValue();
}

Null values

对象工厂:

@XmlRegistry
public class ObjectFactory {

private final static QName _Signature_QNAME = new QName("http://www.w3.org/2000/09/xmldsig#", "Signature");
private final static QName _RetEnviCte_QNAME = new QName("http://www.portalfiscal.inf.br/cte", "retEnviCte");

    /**
     * Create an instance of
     * {@link JAXBElement }{@code <}{@link TRetEnviCTe }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://www.portalfiscal.inf.br/cte", name = "retEnviCte")
    public JAXBElement<TRetEnviCTe> createRetEnviCte(TRetEnviCTe value) {
        return new JAXBElement<TRetEnviCTe>(_RetEnviCte_QNAME, TRetEnviCTe.class, null, value);
    }
}

有关可能导致此问题的原因的任何想法?

谢谢!

0 个答案:

没有答案