javajaxbunmarshallingspring-camelxml-binding

What should be the jaxb annotations for this xml file?


I have a xml file payload which i want to unmarshal using jaxb, I've created a pojo class for unmarshalling and I've defined xml attributes and elements to that pojo, but i'm a little bit confused about namespaces, how to annotate them?

My xml file:

<ns1:ContractLinkEvent xmlns:ns0="http://Enterprise.BizTalk.Canonical.Schemas/v2.0/ESB" xmlns:ns1="http://Enterprise.BizTalk.MCF.Core.Schemas/v2.0/ESB">

<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

</Header>

<ContractLink xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<ContractLinkId>1509148</ContractLinkId>

<BillingProfile>

<BillingProfileId>173886</BillingProfileId>

<BillingProfileCode xsi:nil="true"/>

</BillingProfile>

</ContractLink>

</ns1:ContractLinkEvent>

My Jaxb annotated Pojo is:

@XmlRootElement(name = "ContractLinkEvent", namespace="http://Enterprise.BizTalk.Canonical.Schemas/v2.0/ESB")
@XmlAccessorType(XmlAccessType.FIELD)
public class ContractLinkPojo { 

    @XmlElement(name="Header")
    private String Header;

    @XmlElement(name="ContractLink")
    private String ContractLink;

. . . goes on

I'm getting the following exception while unmarshalling:

java.io.IOException: javax.xml.bind.UnmarshalException
 - with linked exception:
[com.sun.istack.SAXParseException2; lineNumber: 1; columnNumber: 1; unexpected element (uri:"http://Enterprise.BizTalk.MCF.Core.Schemas/v2.0/ESB", local:"ContractLinkEvent"). Expected elements are (none)]

I don't think i've defined the namespaces correctly because i haven't yet defined namespaces as i' still confused, any ideas?

EDIT: This is my routing for unmarshalling

rest("/readXml")
    .consumes("application/xml")
    .post()
    .to("direct:xmlread");

    from("direct:xmlread").streamCaching()
    .doTry().unmarshal(xmlDataFormat)
    .process(readAndInsertXml)
    .to("mock:result").end();
}

Solution

  • Namespaces are similar to package names in Java. You use that to have an unique name for XML elements you create so that they do not conflict with other XML elements. You can define a default XML namespace as:

    xmlns="http://Enterprise.BizTalk.Canonical.Schemas/v2.0/ESB"
    

    You can define multiple XML namespace with prefixes as:

       xmlns:ns0="..." 
       xmlns:ns1="..."
    

    In the above ns0 and ns1 are prefixes you created.

    In your xml, you have not defined any default namespace. So, I guess you are trying to use namespace prefixes ns0 and ns1 to identity elements. In such a case, you have used ns1 for ContractLinkElement but not < Header >, < ContractLink > or anything else. However, ns0 is not used anywhere and if not required, you can remove that. Please check if this is what you intended to do.

    Also, there is no ending tag:

    <ns1:ContractLinkEvent 
    xmlns:ns0="http://Enterprise.BizTalk.Canonical.Schemas/v2.0/ESB" 
    xmlns:ns1="http://Enterprise.BizTalk.MCF.Core.Schemas/v2.0/ESB">