Nagendra Busam
Nagendra Busam

Reputation: 255

XML (with namespace) to Object unmarshalling

I got following repsonse from a Web service call, I tried to unmarshal the same using JAXB to map it to a java class. I was getting unmarshal exception while doing so.

<?xml version="1.0" encoding="UTF-8"?>
<ns0:QueryByLNResponse xmlns:ns0="UIS_CTMPeople_WS" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ns0:getListValues>
        <ns0:First_Name>Pradeep</ns0:First_Name>
        <ns0:Internet_E-mail/>
        <ns0:ManagersName/>
        <ns0:Person_ID>PPL1</ns0:Person_ID>
        <ns0:Last_Name>Srinivasa Reddy</ns0:Last_Name>
        <ns0:Full_Name>Pradeep M Srinivasa Reddy</ns0:Full_Name>
    </ns0:getListValues>
    <ns0:getListValues>
        <ns0:First_Name>Geeth </ns0:First_Name>
        <ns0:Internet_E-mail>[email protected]</ns0:Internet_E-mail>
        <ns0:ManagersName/>
        <ns0:Person_ID>PPL2</ns0:Person_ID>
        <ns0:Last_Name>Srinivasan</ns0:Last_Name>
        <ns0:Full_Name>Geeth  Srinivasan</ns0:Full_Name>
    </ns0:getListValues>
</ns0:QueryByLNResponse>

I tried to unmarshal the above code using

public static Object xmlToObject(String xml, Class... objClass) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(objClass);
    final Unmarshaller unmarshaller = jc.createUnmarshaller();
    return unmarshaller.unmarshal(new StringReader(xml.toString()));
}

It was throwing following error

javax.xml.bind.UnmarshalException: unexpected element (uri:"UIS_CTMPeople_WS", local:"QueryByLNeResponse"). Expected elements are (none)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)

How can i unmarshalling this using JAXB ( xml to object ).

Upvotes: 6

Views: 17435

Answers (1)

bdoughan
bdoughan

Reputation: 149007

Below are a few items that should help:


NAMESPACES

You should use a the @XmlSchema annotation on the package-info class to specify the namespace qualification. Below is an example, you will need to change the package name to match your model.

package-info.java

@XmlSchema(
    namespace = "UIS_CTMPeople_WS",
    elementFormDefault = XmlNsForm.QUALIFIED)
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

For More Information


ROOT ELEMENTS

It appears that you do not have any of your classes mapped with @XmlRootElement (or @XmlElementDecl). I would expect you to have something like the following:

QueryByLNResponse

package example;

@XmlRootElement(name="QueryByLNResponse")
public class QueryByLNResponse {
}

Alternatively you could specify the class you wish to unmarshal to, by using one of the unmarshal methods that take a Class parameter:

return unmarshaller.unmarshal(xml, QueryByLNResponse.class)

For More Information


PERFORMANCE

In your same code you are creating a new JAXBContext each time you do an unmarshal. JAXBContext is a thread safe object which can be created once and reused to improve performance.

Upvotes: 7

Related Questions