Getting list <String> from XML using JAXB (Moxy)

I am using jdk1.6 and eclipselink-2.3.2.v20111125-r10461

I have XML similar to:



<alpha>
     <beta>
            <omega>abcd</omega>
            <gamma>
                <delta>001</delta>
                <delta>002</delta>
                <delta>003</delta>
            </gamma>
     </beta>
</alpha>

      

code>

My annotated bean looks like this:



@XmlRootElement(name = "alpha")
@XmlAccessorType(XmlAccessType.FIELD)
public Class Alpha {

    @XmlPath("beta/omega/text()")
    private String omegaValue;

    @XmlElement(name = "delta")
    @XmlElementWrapper(name = "gamma")
    private List<String> deltaList;

    public String getOmegaValue() {
        return omegaValue;
    }

    public void setOmegaValue(String omegaValue) {
        this.omegaValue = omegaValue;
    }

    public List<String> getDeltaList() {
        return deltaList;
    }

    public void setDeltaList (List<String> deltaList ) {
        this.deltaList = deltaList ;
    }
}

      

code>

in my test code i do the following to check the list but it is empty.

System.out.println(alphaInstance.getDeltaList().size());//returns 0!!
for (String deltaValue: alphaInstance.getDeltaList()) {//empty!!
    System.out.println(deltaValue);
}

      

code>

Is there something wrong here? OmegaValue works great. Please help, thanks.

+3


source to share


1 answer


Your mapping for the property is deltaList

missing a beta level. You can use annotation @XmlPath

and do the following:

@XmlPath("beta/gamma/delta/text()")
private List<String> deltaList;

      



Additional Information

+2


source







All Articles