我想将以下 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;
}