简体   繁体   中英

List coming back as null after unmarshalling into a POJO using JAXB

I am trying to unmarshall an XML into Java POJO using JAXB, all elements are getting unmarshalled fine except for the lists(listId1 and listId2), following is the xml, the pojo classes and the business class.

<?xml version="1.0" encoding="UTF-8"?>
<abacus-1.0-snapshot xmlns="urn:xxxxxxx">
    <rules>
        <rule>
            <name>rule1</name>
            <errorType>WARNING</errorType>
            <flag>true</flag>
            <startDate>2020-05-19</startDate>
            <listId1>
                <id>100101</id>
                <id>100102</id>
            </listId1>
            <listId2>
                <id>100103</id>
                <id>100104</id>
            </listId2>
        </rule>
        <rule>
            <name>rule2</name>
            <errorType>ERROR</errorType>
            <flag>false</flag>
            <startDate>2020-05-20</startDate>
            <listId1>
                <id>100105</id>
                <id>100106</id>
            </listId1>
            <listId2>
                <id>100107</id>
                <id>100107</id>
            </listId2>
        </rule>
    </rules>
</abacus-1.0-snapshot>

The Rules.java which maps the <rules> element to Rules.java

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "rules", namespace = "urn:xxxxxxx")
public class RulesPOJO
{

    @XmlElement(name = "rule", namespace = "urn:xxxxxxx")
    private final List<rulePOJO> rulesPOJO = new ArrayList<rulePOJO>();

    public List<rulePOJO> getRulesPOJO()
    {
        return rulesPOJO;
    }
}

The Rule.java class which is used to map the <rule> element to Rule.java

@XmlAccessorType(XmlAccessType.NONE)
public class RulePOJO
{

    @XmlElement(namespace = "urx:xxxxx")
    private final String name = null;
     
    @XmlElement(namespace = "urx:xxxxx")
    private final String errorType = null;
    
    @XmlElement(namespace = "urx:xxxxx")
    private final Date startDate = null;
    
    @XmlElement(name = "listId1", namespace = "urx:xxxxx")
    private final List<Long> listId1 = new ArrayList<>();
    
    @XmlElement(name = "listId2", namespace = "urx:xxxxx")
    private final List<Long> listId2 = new ArrayList<>();

    public String getName()
    {
        return name;
    }

    public errorType getErrorType()
    {
        return errorType;
    }

    public Boolean getFlag()
    {
        return flag;
    }

    public Date getStartDate()
    {
        return startDate;
    }

    public List<Long> getListId1()
    {
        return listId1;
    }

    public List<Long> getListId2()
    {
        return listId2;
    }
}

the business class used for unmarshalling xml into the pojo

public class Retriever() throws JAXBException
    {
        Document document = Configuration.load(URI.create("urn:xxxxxx"));

        Unmarshaller unmarshaller = JAXBContext.newInstance(RulesPOJO.class).createUnmarshaller();
        RulesPOJO rulesPOJO = (RulesPOJO) unmarshaller
                .unmarshal(document.getElementsByTagName("Rules").item(0));
        for(RulePOJO rulePOJO : RulesPOJO.getRulesPOJO())
        {
            // the following are coming back null
            List<Long> listId1 = rulePOJO.getListId1();
            List<Long> listId2 = rulePOJO.getListId2();
        }
    }

Thanks!

As discussed in the comments, id is the repeating element to be treated as arraylist so as mentioned in the given link https://howtodoinjava.com/jaxb/xmlelementwrapper-annotation/ . You can use XMLElementWrapper and XMLElement types of annotations to denote wrapper and child elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM