JAXB为包含集合的嵌套xml返回null

时间:2013-02-05 19:30:43

标签: java jaxb

我想将以下xml解组为另一个父对象,如下所示。但它总是返回NULL。

传入XML:

<contentFiles>
<contentFile>
<contentFileName>cwb_reg_content_IB20C0F504A9A11E281E4C8BF76F4977C.pdf</contentFileName>
<title><![CDATA[SEC No-Action Guidance Expanding the Definition of “Ready Market” for Certain Foreign Equity Securities]]></title>
<sourcePublicationDate>20121219</sourcePublicationDate>
<alternateDocNumbers>
  <alternateDocNumber>12345-b</alternateDocNumber>
</alternateDocNumbers>
<citesAffected>
   <cite>SEA Rule 15c3-1</cite>
</citesAffected>
</contentFile>
</contentFiles>    

<contentFiles>

对应的父类
 @XmlRootElement(name = "contentFiles")
    public class RtSuperQuickMetadata
    {
        private List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems;

        public RtSuperQuickMetadata()
        {
            rtSuperQuickMetadataItems = new ArrayList<RtSuperQuickMetadataItem>();
        }

        public List<RtSuperQuickMetadataItem> getRtSuperQuickMetadataItems()
        {
            return rtSuperQuickMetadataItems;
        }

        public void setRtSuperQuickMetadataItems(
            List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems)
        {
            this.rtSuperQuickMetadataItems = rtSuperQuickMetadataItems;
        }

    }

<contentFile>

对应的父类
@XmlRootElement(name = "contentFile")
public class RtSuperQuickMetadataItem
{
    private String contentFileName;
    private String title;
    private String sourcePublicationDate;
    private List<AlternateDocNumber> alternateDocNumbers;
    private List<Cite> citesAffected;

    public RtSuperQuickMetadataItem()
    {
        alternateDocNumbers = new ArrayList<AlternateDocNumber>();
        citesAffected = new ArrayList<Cite>();
    }


    public List<AlternateDocNumber> getAlternateDocNumbers()
    {
        return alternateDocNumbers;
    }


    public List<Cite> getCitesAffected()
    {
        return citesAffected;
    }


    public String getContentFileName()
    {
        return contentFileName;
    }


    public String getSourcePublicationDate()
    {
        return sourcePublicationDate;
    }

    public String getTitle()
    {
        return title;
    }


    public void setAlternateDocNumbers(List<AlternateDocNumber> alternateDocNumbers)
    {
        this.alternateDocNumbers = alternateDocNumbers;
    }


    public void setCitesAffected(List<Cite> citesAffected)
    {
        this.citesAffected = citesAffected;
    }


    public void setContentFileName(String contentFileName)
    {
        this.contentFileName = contentFileName;
    }


    public void setSourcePublicationDate(String sourcePublicationDate)
    {
        this.sourcePublicationDate = sourcePublicationDate;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

}


@XmlRootElement(name = "alternateDocNumber")
class AlternateDocNumber
{
    private String alternateDocNumber;

    public String getAlternateDocNumber()
    {
        return alternateDocNumber;
    }

    public void setAlternateDocNumber(String alternateDocNumber)
    {
        this.alternateDocNumber = alternateDocNumber;
    }

    @Override
    public String toString()
    {
        return "AlternateDocNumber [alternateDocNumber=" + alternateDocNumber + "]";
    }
}


@XmlRootElement(name = "cite")
class Cite
{
    private String cite;

    public String getCite()
    {
        return cite;
    }

    public void setCite(String cite)
    {
        this.cite = cite;
    }

    @Override
    public String toString()
    {
        return "Cite [cite=" + cite + "]";
    }
}

使用JAXB的Unmarshaller代码:

public RtSuperQuickMetadata unmarshallXml(final File metadataFile)
        throws JAXBException, FileNotFoundException
    {
        RtSuperQuickMetadata rtSuperQuickMetadata = null;
        try
        {
            JAXBContext jc = JAXBContext.newInstance(RtSuperQuickMetadata.class);

            Unmarshaller um = jc.createUnmarshaller();
            rtSuperQuickMetadata =
                (RtSuperQuickMetadata) um.unmarshal(metadataFile);
        }
        catch (JAXBException e)
        {
            String msg = "Malformed XML supplied as Metadata" + " Msg " + e.getMessage();
            LOG.error(msg, e);
            throw new RuntimeException(msg, e);
        }

        return rtSuperQuickMetadata;
    }

2 个答案:

答案 0 :(得分:3)

你有太多XmlRootElements,你通常只希望将它与top元素一起使用。您要做的是将孩子标记为XmlElement

从除根元素之外的所有元素(即XmlRootElement)中删除RtSuperQuickMetadata注释,并在其将加载它们的类中使用XmlElement 标记它们

因此,作为一个例子,以下是RtSuperQuickMetadata类的外观:

@XmlRootElement(name = "contentFiles")
class RtSuperQuickMetadata
{
    private List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems;

    public RtSuperQuickMetadata()
    {
        rtSuperQuickMetadataItems = new ArrayList<RtSuperQuickMetadataItem>();
    }

    @XmlElement(name = "contentFile")
    public List<RtSuperQuickMetadataItem> getRtSuperQuickMetadataItems()
    {
        return rtSuperQuickMetadataItems;
    }

    public void setRtSuperQuickMetadataItems(
            List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems)
    {
        this.rtSuperQuickMetadataItems = rtSuperQuickMetadataItems;
    }

}

将此原则转移到alternateDocNumberscitesAffected


如果您想查看Unmarshaller如何根据您的注释对XML进行格式化的示例,您可以在代码中创建结构并使用Marshaller。这是一个快速而丑陋的例子:

RtSuperQuickMetadata rtSuperQuickMetadata = new RtSuperQuickMetadata();

List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems = new ArrayList<RtSuperQuickMetadataItem>();
rtSuperQuickMetadata.setRtSuperQuickMetadataItems(rtSuperQuickMetadataItems);

RtSuperQuickMetadataItem item = new RtSuperQuickMetadataItem();
rtSuperQuickMetadataItems.add(item);

ArrayList<Cite> cites = new ArrayList<Cite>();
Cite cite = new Cite();
cite.setCiteStr("MyCite");
cites.add(cite);
item.setCitesAffected(cites);

Marshaller m = jaxbContext.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(rtSuperQuickMetadata, System.out);

这会将结果输出到System.out。你可以把它放在一个文件中,不管你需要什么。

答案 1 :(得分:1)

我已添加此答案,以解决您在answer given by cklab上发表的评论后续问题。

  

我有另一个问题。为什么jaxb不会自动分配内存   对于包含在对象中的列表。为什么我们需要分配内存   在它的构造函数中?

你不需要,见下文。

  

在解组这些项目时,以下内容未保存到   元数据对象。 12我   需要添加另一个元素才能生成?

请参阅下面的地图。

<强> RtSuperQuickMetadata

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement(name = "contentFiles")
public class RtSuperQuickMetadata {

    private List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems;

    @XmlElement(name="contentFile")
    public List<RtSuperQuickMetadataItem> getRtSuperQuickMetadataItems() {
        return rtSuperQuickMetadataItems;
    }

    public void setRtSuperQuickMetadataItems(
            List<RtSuperQuickMetadataItem> rtSuperQuickMetadataItems) {
        this.rtSuperQuickMetadataItems = rtSuperQuickMetadataItems;
    }

}

<强> RtSuperQuickMetadataItem

import java.util.List;
import javax.xml.bind.annotation.XmlType;

@XmlType(propOrder={"contentFileName", "title", "sourcePublicationDate", "alternateDocNumbers", "citesAffected"})
public class RtSuperQuickMetadataItem {
    private String contentFileName;
    private String title;
    private String sourcePublicationDate;
    private List<AlternateDocNumber> alternateDocNumbers;
    private List<Cite> citesAffected;

    public List<AlternateDocNumber> getAlternateDocNumbers() {
        return alternateDocNumbers;
    }

    public List<Cite> getCitesAffected() {
        return citesAffected;
    }

    public String getContentFileName() {
        return contentFileName;
    }

    public String getSourcePublicationDate() {
        return sourcePublicationDate;
    }

    public String getTitle() {
        return title;
    }

    public void setAlternateDocNumbers(
            List<AlternateDocNumber> alternateDocNumbers) {
        this.alternateDocNumbers = alternateDocNumbers;
    }

    public void setCitesAffected(List<Cite> citesAffected) {
        this.citesAffected = citesAffected;
    }

    public void setContentFileName(String contentFileName) {
        this.contentFileName = contentFileName;
    }

    public void setSourcePublicationDate(String sourcePublicationDate) {
        this.sourcePublicationDate = sourcePublicationDate;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}

<强> AlternateDocNumber

class AlternateDocNumber {

    private String alternateDocNumber;

    public String getAlternateDocNumber() {
        return alternateDocNumber;
    }

    public void setAlternateDocNumber(String alternateDocNumber) {
        this.alternateDocNumber = alternateDocNumber;
    }

}

<强>引用

class Cite {

    private String cite;

    public String getCite() {
        return cite;
    }

    public void setCite(String cite) {
        this.cite = cite;
    }

}