Получить нулевое значение с Jaxb Unmarshall - PullRequest
0 голосов
/ 15 мая 2018

Я хочу отобразить XML-файл в Java-объект, используя библиотеку Jaxb. У меня есть схема для проверки 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);

Когда я запускаю приведенный выше код, я получаю все остальные данные, кроме данных метаданных. Возвращает нулевое значение метаданные dc метаданных. Я думаю, что есть проблема с подсхемой oai_dc: dc . Но я не знаю, как это исправить. Пожалуйста, дайте мне несколько советов, спасибо.

1 Ответ

0 голосов
/ 14 августа 2018

Я использовал библиотеку maven-jaxb2-plugin и изменил XmlElementRef на XmlElement. Это сработало.

...