簡體   English   中英

使用 JAXB 解組到 POJO 后,列表返回為 null

[英]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>

將 <rules> 元素映射到 Rules.java 的 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;
    }
}

Rule.java class 用於 map <rule> 元素到 Rule.Z93F725A04423FE1C886ZF

@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;
    }
}

業務 class 用於將 xml 解組到 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();
        }
    }

謝謝!

正如評論中所討論的, id是被視為 arraylist 的重復元素,如給定鏈接https://howtodoinjava.com/jaxb/xmlelementwrapper-annotation/中所述。 您可以使用XMLElementWrapperXMLElement類型的注釋來表示包裝器和子元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM