Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB Unmarshals Nested Element to Null

XML snippet:

<datasource formatted-name="blah" inline="blah">
    <repository-location derived-from="blah" id="blah" path="blah" revision="blah" site="blah"/>
</datasource>

I am trying to unmarshal everything under one class (DataSource) with nested static classes. Here is my DataSource class:

@XmlRootElement(name = "datasource")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataSource {

    @XmlAttribute(name = "formatted-name")
    protected String formattedName;
    @XmlAttribute(name = "inline")
    protected String inline;
    @XmlElement(name = "repository-location")
    protected RepositoryLocation repositoryLocation;

    // public getters and setters for fields above

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class RepositoryLocation {

        @XmlAttribute(name = "derived-from")
        protected String derivedFrom;
        @XmlAttribute(name = "id")
        protected String id;
        @XmlAttribute(name = "path")
        protected String path;
        @XmlAttribute(name = "revision")
        protected String revision;
        @XmlAttribute(name = "site")
        protected String site;

        // public getters and setters for fields above
    }
}

Unmarshaller:

JAXBContext jaxbContext = JAXBContext.newInstance(DataSource.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(responseXML);
dataSourceResponse = (DataSource) unmarshaller.unmarshal(reader);

I can successfully output DataSource fields "formattedName" and "inline", but "repositoryLocation" is null. Can someone help please?

like image 991
toadead Avatar asked Sep 29 '16 21:09

toadead


2 Answers

JAXB is able to unmarshal without Getters/Setters and fields can even be private. Given the DataSource class above, having some generated toString method added to both DataSource and RepositoryLocation, the following prints out all properties:

import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import java.io.StringReader;

public class Jaxb {

    public static void main(String[] args) throws JAXBException {
        String xml = "<datasource formatted-name=\"blah\" inline=\"blah\">\n" +
                "    <repository-location derived-from=\"blah\" id=\"blah\"" +
                " path=\"blah\" revision=\"blah\" site=\"blah\"/>\n" +
                "</datasource>";

        JAXBContext context = JAXBContext.newInstance(DataSource.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        DataSource dataSource = (DataSource) unmarshaller.unmarshal(new StringReader(xml));
        System.out.println(dataSource);
    }

    @XmlRootElement(name = "datasource")
    @XmlAccessorType(XmlAccessType.FIELD)
    private class DataSource {

        @XmlAttribute(name = "formatted-name")
        private String formattedName;
        @XmlAttribute(name = "inline")
        private String inline;
        @XmlElement(name = "repository-location")
        private RepositoryLocation repositoryLocation;

        @XmlAccessorType(XmlAccessType.FIELD)
        private class RepositoryLocation {

            @XmlAttribute(name = "derived-from")
            private String derivedFrom;
            @XmlAttribute(name = "id")
            private String id;
            @XmlAttribute(name = "path")
            private String path;
            @XmlAttribute(name = "revision")
            private String revision;
            @XmlAttribute(name = "site")
            private String site;

            @Override
            public String toString() {
                return "RepositoryLocation{" +
                        "derivedFrom='" + derivedFrom + '\'' +
                        ", id='" + id + '\'' +
                        ", path='" + path + '\'' +
                        ", revision='" + revision + '\'' +
                        ", site='" + site + '\'' +
                        '}';
            }
        }

        @Override
        public String toString() {
            return "DataSource{" +
                    "formattedName='" + formattedName + '\'' +
                    ", inline='" + inline + '\'' +
                    ", repositoryLocation=" + repositoryLocation +
                    '}';
        }
    }
}
like image 100
ldz Avatar answered Nov 15 '22 15:11

ldz


Problem solved! I noticed my java 1.7 library is missing some jars. So I decided to go ahead upgrade to 1.8. It worked like magic!

like image 36
toadead Avatar answered Nov 15 '22 15:11

toadead