prasad kp
prasad kp

Reputation: 861

Unmarshaling the nested xml data giving null values

    <bas:person>

             <req:vehicleinfo>
                <!--Zero or more repetitions:-->
                <bas:item>
                   <sch:modelno>k10</sch:modelno>
                   <sch:type>bs4</sch:type>
                </bas:item>
 <bas:item>
                   <sch:modelno>k12</sch:modelno>
                   <sch:type>bs5</sch:type>
                </bas:item>
             </req:extensionInfo>
    </bas:person>

Assume namespace for bas is some http://xxxxx.person.com
 for req is http://xxxxx.request.com
 for bas http://xxxxx.bas.com
for sch http://xxxxx.sch.com

Here for vehicle info as i may have multiple values, in my Java object i need to have vehicle info as array do to some project dependencies.

Here is my java class

 @XmlRootElement(name="person")
@XmlAccessorType(XmlAccessType.FIELD)
    public class Person implements Serializable{


        @XmlElement(name="vehicleinfo",namespace="http://xxxxx.request.com")
        private VehicleInfo[] vehicleinfo;


// getter & setter

    }

vehicle Info

@XmlAccessorType(XmlAccessType.FIELD)
    public class  VehicleInfo{

        @XmlAttribute(name="modelno",namespace="http://xxxxx.sch.com")
        private String key;
        @XmlAttribute(name="type",namespace="http://xxxxx.sch.com")
        private String value;

 //getter and setter

    }

i am using the following code to unmarshalling

SOAPMessage message = MessageFactory.newInstance().createMessage(null,
                new ByteArrayInputStream(xmlInput.getBytes()));
        JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Person person= (Person) jaxbUnmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());

but m getting key and value as nul for vehicle info

Upvotes: 0

Views: 262

Answers (1)

Vadim
Vadim

Reputation: 4120

JAXB does not work with arrays by nature because Java does not have dynamic arrays. Instead declare your VehicleInfo as ArrayList.

Upvotes: 1

Related Questions