Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

java - 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!

question from:https://stackoverflow.com/questions/65891448/list-coming-back-as-null-after-unmarshalling-into-a-pojo-using-jaxb

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...