当前位置 : 主页 > 网页制作 > xml >

名称空间 – 用于对象解组的XML(带名称空间)

来源:互联网 收集:自由互联 发布时间:2021-06-13
我从Web服务调用中得到了以下repasonse,我试图使用JAXB将其映射到 java类.这样做时我得到了解组异常. ?xml version="1.0" encoding="UTF-8"?ns0:QueryByLNResponse xmlns:ns0="UIS_CTMPeople_WS" xmlns:soapenv="http://s
我从Web服务调用中得到了以下repasonse,我试图使用JAXB将其映射到 java类.这样做时我得到了解组异常.

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

我尝试使用解组上面的代码

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

它抛出以下错误

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)

我如何使用JAXB(xml到对象)解组它.

以下是一些应该有用的项目:

NAMESPACES

您应该在package-info类上使用@XmlSchema批注来指定命名空间限定.下面是一个示例,您需要更改包名称以匹配您的模型.

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;

欲获得更多信息

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

根元素

您似乎没有使用@XmlRootElement(或@XmlElementDecl)映射任何类.我希望你有类似以下的东西:

QueryByLNResponse

package example;

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

或者,您可以使用一个带有Class参数的unmarshal方法指定要解组的类:

return unmarshaller.unmarshal(xml, QueryByLNResponse.class)

欲获得更多信息

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

性能

在同一代码中,每次执行unmarshal时都会创建一个新的JAXBContext. JAXBContext是一个线程安全对象,可以创建一次并重用以提高性能.

网友评论