1

I have an xml document such as :

<ParentClass>
    <FirstClass>
        <Row>
            <data1>...<data1>
            <data2>...<data2>
        </Row>
        <Row>
            <data1>...<data1>
            <data2>...<data2>
        </Row>
    </FirstClass>   
    <SecondClass>
        <Row>
            <data1>...<data1>
            <data2>...<data2>
        </Row>
        <Row>
            <data1>...<data1>
            <data2>...<data2>
        </Row>
    </SecondClass>
    </ParentClass>

I have the java classes as below:

@XmlRootElement(name = "ParentClass")
@XmlAccessorType(XmlAccessType.FIELD)
public class ParentJavaClass
{
    @XmlElement(name="FirstClass")
    private List<FirstJavaClass> fc;
    public List<FirstJavaClass> getFc()
    {
        return fc;
    }
    public void setFc(List<FirstJavaClass> fc)
    {
        this.fc = fc;
    }

    @XmlElement(name="SecondClass")
    private List<SecondJavaClass> sc;
    public List<SecondJavaClass> getSc()
    {
        return sc;
    }
    public void setSc(List<SecondJavaClass> sc)
    {
        this.sc = sc;
    }
}

    @XmlType(name = "Row", namespace = "Fc")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class FirstJavaClass
    {
    private String data1;
    private String data2;

    // getters & setters
    }

    @XmlType(name = "Row", namespace = "Sc")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class SecondJavaClass
    {
    private String data1;
    private String data2;

    // getters & setters
     }

But, when I call

final ParentJavaClass x = (ParentJavaClass) jaxbUnmarshaller.unmarshal(file);

the resulting lists for both my FirstJavaClass & SecondJavaClass has all null values. I think the "Row" element is what is tripping me, but am not sure how to annotate that correctly. Please assist. Thanks.

1 Answer 1

1

You should annotate as follows:

@XmlElementWrapper(name="FirstClass")
@XmlElement(name="Row")
private List<FirstJavaClass> fc;

@XmlElementWrapper(name="SecondClass")
@XmlElement(name="Row")
private List<SecondJavaClass> sc;

The annotations mean:

  • The @XmlElementWrapper corresponds to the element you want to nest your collection data withing.
  • Each instance of FirstJavaClass is going to correspond to a Row element nested within the FirstClass element.

Debugging Trick

When your JAXB object model does not unmarshal correct, try:

  1. populating it and marshalling it to XML.
  2. then compare this XML to what you are trying to unmarshal to see what the differences are.
  3. adjusting the annotations until the marshalled XML matches what you are trying to unmarshal
  4. unmarshal the XML.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.