简体   繁体   中英

JAXB xml string to java object - unexpected element

Please help!

I am trying to unmarshall the below XML STRING to a java class. 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

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

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:

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.

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

to your @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" ...

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.


EDIT:

In your scenario, I would generate the classes with (built-in) xjc (command line tool ...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)).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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