简体   繁体   English

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

[英]JAXB xml string to java object - unexpected element

Please help!请帮忙!

I am trying to unmarshall the below XML STRING to a java class.我正在尝试将以下 XML STRING 解组为 java 类。 The requirement is only to grab some elements and not all:要求只是抓取一些元素而不是全部:

<?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>

From this file I am only looking to map these tags in my java class below从这个文件中,我只想在下面的 java 类中映射这些标签

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

Java class: Java类:

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

Unmarshalling implementation:解组实现:

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

Error:错误:

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)...

In My gradle dependencies:在我的 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",

And java 11 is the complier.而java 11是编译器。

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

to your @XmlRootElement :到您的@XmlRootElement

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

...this is what the error message complains about: A mismatch in the namespace! ...这就是错误消息所抱怨的:命名空间不匹配!

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

The {} means empty ( "" ) namespace, in this case.在这种情况下, {}表示空 ( "" ) 命名空间。

If namespace omit, it defaults to "##default" ... 如果namespace省略,则默认为"##default" ...

If the value is "##default", then the XML namespace name is derived from the package of the class ( XmlSchema ) .如果值为“##default”,则 XML 命名空间名称派生自类 ( XmlSchema ) 的包 If the package is unnamed, then the XML namespace is the default empty namespace.如果包未命名,则 XML 命名空间是默认的空命名空间。


EDIT:编辑:

In your scenario, I would generate the classes with (built-in) xjc (command line tool ...in ${java.home}/bin )在您的场景中,我将使用(内置)xjc(命令行工具 ...in ${java.home}/bin )生成类

xjc -d generated <schema>

...where gnerated will be the output folder for the classes (.java files) and <schema> can be a file URL or (structured)directory (with your (readonly) xsd(s)). ...其中gnerated将是类(.java 文件)的输出文件夹, <schema>可以是文件 URL 或(结构化)目录(带有您的(只读)xsd(s))。

Once successfully generated, copy the content of generated into your "main source folder", check it in(, use it) and only change it, when xsd changes.成功生成后,将生成的内容复制到您的“主源文件夹”中,将其签入(,使用它)并仅在 xsd 更改时更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM