I am trying to unmarshal nested XML file using XMLStreamReader. My XML file looks like this :
<?xml version="1.0" encoding="UTF-8"?>
<tns:Envelope
xmlns:tns="http://www.w3.org/2003/05/soap-envelope-dial"
xmlns:lmic="http://www.example.com"
xmlns:producer="http://example1.com/"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:ns5="http://www.example.com/dial/3/0">
<tns:header>
...
...
</tns:header>
<tns:body>
<producer:Producer id="1234">
<producer:GenParty>
<producer:NameInfo>
<producer:Comm>
<producer:SuppName>DATA</producer:SuppName>
<producer:ContractNumber>123456</producer:ContractNumber>
</producer:Comm>
</producer:NameInfo>
<producer:Address>
<Street>ABC</Street>
<Country>DEF</Country>
...
...
</prodcer:Address>
<producer:Address>
<Street>ABC</Street>
<Country>DEF</Country>
...
...
</prodcer:Address>
</producer:GenParty>
</producer:Producer>
</tns:body>
</tns:emvelope>
I have created classes like the following:
@XmlRootElement(name="Producer",namespace="http://example.com/")
@XmlAccessorType(XmlAccessType.FIELD)
Class Producer {
private GenParty;
// getter method of class GenParty
// setter method of class GenParty
}
@XmlRootElement(name="GenParty")
@XmlAccessorType(XmlAccessType.FIELD)
class GenParty {
private NameInfo;
private List<Address> address;
//getter of both fields
// setter of both fields
}
and subsequent classes are defined.
I am using XMLStreamReader to advance to the tag and then I am writing my unmarshaler code as:
JAXBContext jc = JAXBContext.newInstance(Producer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Producer producer = unmarshaller.unmarshal(xsr,Producer.class).getValue();
However, I am getting the null value set to on Producer object. Is there anything I am doing wrong? I could unmarshal simple XML files but this level of nesting is creating problems for me. Can someone please suggest easy of doing it or any changes I should make in my code skeleton?
Thanks a lot in advance!
It is a bit hard to say what you are doing wrong. Yet I would suggest to create a
Producer
in code and then marshall and unmarshall it to check, if all your classes are ok.If the classes are ok and marshalling / unmarshalling works the
producer
variable should never be null.Here is an example of how this exercise would look like:
If you execute this code with Java 8 it does not throw any exception.