我想使用Jaxb库将xml文件映射到Java对象。我有一个模式来验证xml。
架构:http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd
我使用eclipse从上面的模式生成Pojo。
OAIPMHtype.java
public class OAIPMHtype {
@XmlElement(name = "ListRecords")
protected ListRecordsType listRecords;
}
ListRecordsType.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ListRecordsType", propOrder = {
"record",
"resumptionToken"
})
public class ListRecordsType {
@XmlElement(required = true)
protected List<RecordType> record;
}
RecordType.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "recordType", propOrder = {
"header",
"metadata",
"about"
})
public class RecordType {
@XmlElement(required = true)
protected HeaderType header;
protected MetadataType metadata;
protected List<AboutType> about;
}
MetadataType.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "metadataType", propOrder = {
"dc"
})
public class MetadataType {
@XmlElement(required = true)
protected OaiDcType dc;
}
OaiDcType.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "oai_dcType", namespace = "http://www.openarchives.org/OAI/2.0/oai_dc/", propOrder = {
"titleOrCreatorOrSubject"
})
public class OaiDcType {
@XmlElementRefs({
@XmlElementRef(name = "identifier", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "subject", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "rights", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "date", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "creator", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "format", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "language", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "source", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "type", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "contributor", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "coverage", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "publisher", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "relation", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "title", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false),
@XmlElementRef(name = "description", namespace = "http://purl.org/dc/elements/1.1/", type = JAXBElement.class, required = false)
})
protected List<JAXBElement<ElementType>> titleOrCreatorOrSubject;
}
示例数据:data.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>2018-05-14T20:23:15Z</responseDate>
<request verb="ListRecords" metadataPrefix="oai_dc">http://example.com:7090/api/oai</request>
<ListRecords>
<record>
<header>
<identifier>oai:example.com:42600</identifier>
<datestamp>2018-01-22</datestamp>
</header>
<metadata>
<oai_dc:dc
xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
<dc:title>Applications of organic and printed electronics :</dc:title>
<dc:subject>Electronics</dc:subject>
<dc:description>
Organic and printed electronics can enable a revolution in the applications of electronics and this book offers readers an overview of the state-of-the-art in this rapidly evolving domain. The potentially low cost, compatibility with flexible substrates and the wealth of devices that characterize organic and printed electronics will make possible applications that go far beyond the well-known displays made with large-area silicon electronics. Since organic electronics are still in their early stage, undergoing transition from lab-scale and prototype activities to production, this book serves as a valuable snapshot of the current landscape of the different devices enabled by this technology, reviewing all applications that are developing and those can be foreseen.
</dc:description>
<dc:publisher>New York</dc:publisher>
<dc:contributor>Cantatore, Eugenio</dc:contributor>
<dc:date>2013</dc:date>
<dc:identifier>
http://example.com/pages/opac/wpid-detailbib-id-42603.html
</dc:identifier>
<dc:language>eng</dc:language>
</oai_dc:dc>
</metadata>
</record>
</ListRecords>
</OAI-PMH>
这是代码,我用来将Xml转换为Object:
JAXBContext jaxbContext = JAXBContext.newInstance(OAIPMHtype.class);
File file = new File("data.xml");
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Object dept = jaxbUnmarshaller.unmarshal(file);
System.out.println(dept);
当我运行上面的代码时,除了元数据数据外,我还获得了所有其他数据。它返回null值元数据的元数据dc。我认为有一个子模式 oai_dc:dc 的问题。但我不知道如何解决它。请给我一些提示,谢谢。
答案 0 :(得分:1)
我使用了maven-jaxb2-plugin库,并将XmlElementRef更改为XmlElement。它奏效了。