JAXB xml 字符串到 java 对象 - 意外元素

JAXB xml string to java object - unexpected element

请帮忙!

我正在尝试将下面的 XML STRING 解组为 java class。要求只抓取部分元素,不抓取全部:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type='text/xsl' href='http://myService/rs/../xsl/searchRetrieveResponse.xsl'?>
<searchRetrieveResponse
    xmlns="http://www.loc.gov/zing/srw/"
    xmlns:srw5="info:srw/extension/5/restrictorSummary">
    <version>1.2</version>
    <numberOfRecords>1</numberOfRecords>
    <records>
        <record>
            <recordSchema>info:srw/schema/1/CDFXML</recordSchema>
            <recordPacking>xml</recordPacking>
            <recordData>
                <institution active="true" test="true" training="false"
                    xmlns="info:rfa/rfaRegistry/xmlSchemas/institution"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="info:rfa/rfaRegistry/xmlSchemas/institution http://worldcat.org/registry/xsd/collections/Institutions/institution.xsd">
                    <identifier>info:rfa/Institutions/113500</identifier>
                    <versionID>2016-02-17T20:01:22.355Z</versionID>
                    <nameLocation
                        xmlns="info:rfa/rfaRegistry/xmlSchemas/institutions/nameLocation"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="info:rfa/rfaRegistry/xmlSchemas/institutions/nameLocation http://worldcat.org/registry/xsd/collections/Institutions/nameLocation.xsd">
                        <lastUpdated>2015-09-27</lastUpdated>
                        <lastUpdatedTime>03:06:43</lastUpdatedTime>
                        <first>First Name</first>
                    </nameLocation>
                    <identifiers
                        xmlns="info:rfa/rfaRegistry/xmlSchemas/institutions/identifiers"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="info:rfa/rfaRegistry/xmlSchemas/institutions/identifiers http://worldcat.org/registry/xsd/collections/Institutions/identifiers.xsd">
                        <lastUpdated>2016-02-17</lastUpdated>
                        <lastUpdatedTime>15:01:22</lastUpdatedTime>
                        <age>23</age>
                        <age>55</age>
                    </identifiers>
                    <opac available="true" intranetOnly="false"
                        xmlns="info:rfa/rfaRegistry/xmlSchemas/institutions/opac"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="info:rfa/rfaRegistry/xmlSchemas/institutions/opac http://worldcat.org/registry/xsd/collections/Institutions/opac.xsd">
                        <lastUpdated>2009-12-03</lastUpdated>
                        <lastUpdatedTime>17:43:52</lastUpdatedTime>
                        <url1>facebook</url1>
                        <url2>google</url2>
                        <prefix/>
                    </opac>
                </institution>
            </recordData>
            <recordPosition>1</recordPosition>
        </record>
    </records>
</searchRetrieveResponse>

从这个文件中,我只想将这些标签映射到我下面的 java class

<first> <age> <age> <url1> <url2>

Java class:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "searchRetrieveResponse")
    public class MyInfo {
        @XmlElement(name = "first")
        private String first;

        @XmlElement(name = "age")
        private List<String> age;

        @XmlElement(name = "url1")
        private String url1;

        @XmlElement(name = "url2")
        private String url2;
    }

解组实现:

public static void main(String[] args) {

    JAXBContext jaxbContext;
    try {
        jaxbContext = JAXBContext.newInstance(MyInfo.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        MyInfo myInfo = (MyInfo) jaxbUnmarshaller.unmarshal(
                     new StringReader( * * MY_XML_STRING **));

    catch(JAXBException ex){
            log.error("Error - ", ex);
        }
    }

错误:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.loc.gov/zing/srw/", local:"searchRetrieveResponse"). Expected elements are <{}searchRetrieveResponse>
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:744)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:262)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1149)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:574)...

在我的 gradle 依赖项中:

jaxbApi  : "javax.xml.bind:jaxb-api:2.4.0-b180830.0359",
jaxbImpl : "com.sun.xml.bind:jaxb-impl:2.4.0-b180830.0438",
jaxbCore : "org.glassfish.jaxb:jaxb-core:2.3.0.1",

而java 11是编译器。

添加:, namespace = "http://www.loc.gov/zing/srw/"

给你的 @XmlRootElement:

 @XmlRootElement(name = "searchRetrieveResponse", namespace = "http://www.loc.gov/zing/srw/")

...这就是错误消息所抱怨的内容:名称空间不匹配!

unexpected element (uri:"http://www.loc.gov/zing/srw/", local:"searchRetrieveResponse").
Expected elements are <{}searchRetrieveResponse>

在本例中,{} 表示空 ("") 命名空间。

If namespace omit, it defaults to "##default"...

If the value is "##default", then the XML namespace name is derived from the package of the class ( XmlSchema ). If the package is unnamed, then the XML namespace is the default empty namespace.


编辑:

在您的场景中,我将使用(内置)xjc(命令行工具 ...在 ${java.home}/bin

生成 类
xjc -d generated <schema>

...其中 gnerated 将是 类(.java 文件)的输出文件夹,<schema> 可以是文件 URL或(结构化)目录(与您的(只读)xsd(s)).

一旦成功生成,将生成的内容复制到您的"main source folder"中,将其签入(,使用)并仅在xsd更改时更改它。