1

am using jaxb for converting xml response to java object, i have tried but am getting null for nested class object.

XML String

<person>
    <name>name</name>
    <age>12</age>
    <address>
        <info>
            <contactadress>
                <city>
                </city>
                <phone>
                </phone>
            </contactadress>
        </info>
    </address>
</person> 

mapping java class

@XmlRootElement(name = "person")
public class person
{
    @XmlElement(name = "name")
    String name;

    @XmlElement(name = "age")
    int age:

    @XmlElement(name = "address/info/contactadress")
    person.Address address;

    @XmlRootElement(name = "contactadress")
    public static class Address{

        @XmlElement(name = "city")
        String city;

        @XmlElement(name = " phone")
        String phone;
    }
}

JaxB code:

public Person parseXml(String xmlResponse, Person pserson)
{

 StringReader stringReader = new StringReader(xmlResponse);
            JAXBContext jaxbContext = JAXBContext.newInstance(pserson.class);
            XMLInputFactory xif = XMLInputFactory.newInstance();
            XMLStreamReader xsr = xif.createXMLStreamReader(stringReader);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            return unmarshaller.unmarshal(xsr);
}

after converstion am getting address object is null.

venu
  • 2,971
  • 6
  • 40
  • 59

2 Answers2

0

your Person class should look like this

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"age",
"address"
})
@XmlRootElement(name = "person")
public class Person {

@XmlElement(required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "NCName")
protected String name;
@XmlElement(required = true)
protected BigInteger age;
@XmlElement(required = true)
protected Address address;

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

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

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

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

/**
 * Gets the value of the address property.
 * 
 * @return
 *     possible object is
 *     {@link Address }
 *     
 */
public Address getAddress() {
    return address;
}

/**
 * Sets the value of the address property.
 * 
 * @param value
 *     allowed object is
 *     {@link Address }
 *     
 */
public void setAddress(Address value) {
    this.address = value;
}

}
LMK
  • 2,882
  • 5
  • 28
  • 52
0

The @XmlElement annotation does not allow you to specify a path as the element name as you have done in your question. Instead you would need classes for each level of nesting. These classes could be leveraged via an XmlAdapter if you wanted (see: Access attribute of internal element in the most simple way).

@XmlElement(name = "address/info/contactadress")
person.Address address;

In EclipseLink JAXB (MOXy) we do offer an @XmlPath extension that lets you do this:

@XmlPath("address/info/contactadress")
person.Address address;

I have written more about this use case on my blog:

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400