XML (with namespace) to Object unmarshalling

落爺英雄遲暮 提交于 2020-07-06 12:16:11

问题


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>bas@yahoo.com</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 ).


回答1:


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

  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

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

  • http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html

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.



来源:https://stackoverflow.com/questions/11843142/xml-with-namespace-to-object-unmarshalling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!