1

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.

AMagic
  • 2,690
  • 3
  • 21
  • 33

1 Answers1

1

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.

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • Thank you. Yes, that was the issue. But now I am getting only nulls in all the fields. – AMagic Jan 21 '19 at 23:37
  • ok I see! :) (i was hoping it was the last issue :( ) ...but the namespaces inside are completely "mixed" :) – xerx593 Jan 21 '19 at 23:39
  • ..so as i understand the xml/xsd is fixed, and you (are free to) develop the jaxb mapping?? [...use XJC!](https://stackoverflow.com/a/11463312/592355) – xerx593 Jan 21 '19 at 23:42
  • My actual tag looks like this `` Do I need to add more things to the annotation? – AMagic Jan 21 '19 at 23:43
  • You understood just right. The xml is always fixed and i am trying to map the needed fields. – AMagic Jan 21 '19 at 23:45
  • [XJC!](https://docs.oracle.com/javase/8/docs/technotes/tools/unix/xjc.html) – xerx593 Jan 21 '19 at 23:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187085/discussion-between-amagic-and-xerx593). – AMagic Jan 21 '19 at 23:51