简体   繁体   中英

Jaxb: namespace unmarshalling issue

all, I have a package full of Beans generated from xsd with Jaxb . I want to use some of them into another Bean to marshal/unmarshal an XML like this:

<WrongDocument>
    <test>test label</test>
    <CBISDDReqLogMsg xmlns="urn:CBI:xsd:CBISDDReqLogMsg.00.01.00">
        <GrpHdr>
          ....
        </GrpHdr>
        <PmtInf>
        </PmtInf>
    </CBISDDReqLogMsg>
</WrongDocument>

the root Bean is

@XmlRootElement(name="WrongDocument")
@XmlType(name = "", propOrder = {
  "test",
  "CBISDDReqLogMsg"
})
@XmlAccessorType(XmlAccessType.FIELD)
public class WrongDocumentDTO implements Serializable {

 private static final long serialVersionUID = 8545918230166653233L;

 @XmlElement(required = true, type = String.class, nillable = true)
    protected String test;

 @XmlElement(required = true)
 protected CBISDDReqLogMsg000100 CBISDDReqLogMsg;

 ....

}

and the CBISDDReqLogMsg000100 is

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "CBISDDReqLogMsg.00.01.00", propOrder = {
    "grpHdr",
    "pmtInf"
})
public class CBISDDReqLogMsg000100
    implements Serializable
{

    private final static long serialVersionUID = 1L;
    @XmlElement(name = "GrpHdr", required = true)
    protected CBIGroupHeader2 grpHdr;
    @XmlElement(name = "PmtInf", required = true)
    protected List<PaymentInstructionInformation2> pmtInf;

    ....

}

For CBISDDReqLogMsg000100 namespace is defined with package-info file.

This is the code for unmarshalling:

jc = JAXBContext.newInstance(WrongDocumentDTO.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
WrongDocumentDTO wrongDocumentDTO = unmarshaller.unmarshal(source, WrongDocumentDTO.class).getValue();

Unfortunately, inside my wrongDocumentDTO i have test field populated with the right value, however CBISDDReqLogMsg is null.

How can i resolve this issue?

thank you in advance

You will need to specify the namespace specifically on the element if it is not default for the whole package .

@XmlElement(required = true, namespace = "urn:CBI:xsd:CBISDDReqLogMsg.00.01.00")
protected CBISDDReqLogMsg000100 CBISDDReqLogMsg;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM