4

我想将以下 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 解组器代码:

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

2 回答 2

3

您有太多XmlRootElements,您通常只想将其与顶部元素一起使用。您要做的是将孩子标记为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. 你可以把它放在一个文件中,无论你需要什么。

于 2013-02-05T19:40:55.257 回答
1

我已添加此答案以解决您发布的后续问题,作为对cklab 给出的答案的评论

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

你不需要,见下文。

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

请参阅下面的映射。

RtSuperQuick元数据

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

}
于 2013-02-06T14:26:56.490 回答