【问题标题】:XML (with namespace) to Object unmarshallingXML(带命名空间)到对象解组
【发布时间】:2012-08-07 09:38:11
【问题描述】:

我从 Web 服务调用中得到了以下响应,我尝试使用 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 jaxb unmarshalling


    【解决方案1】:

    以下是一些应该有所帮助的项目:


    命名空间

    您应该在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;
    

    更多信息


    根元素

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

    QueryByLNResponse

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

    或者,您可以指定要解组的类,方法是使用采用Class 参数的解组方法之一:

    return unmarshaller.unmarshal(xml, QueryByLNResponse.class)
    

    更多信息


    性能

    在您的相同代码中,您每次执行解组时都会创建一个新的JAXBContextJAXBContext 是一个线程安全的对象,可以创建一次并重复使用以提高性能。

    【讨论】:

    • 非常感谢您的快速回复,我会试一试并告诉您结果
    • 我在 QueryByLNResponse 根元素下有记录列表(getListValues),我需要如何在 java 中处理这个
    • @user879870 - 以下内容应该有所帮助:blog.bdoughan.com/2010/09/jaxb-collection-properties.html
    • 如果我们使用 XML 注释,属性不需要设置器和获取器?
    • 您的博客非常有用。我是 JAXB 的新手——真的很有帮助。我还在做这个,如果有什么问题我会在这里发布
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2021-08-26
    • 2010-11-08
    • 2021-10-24
    • 2020-10-11
    相关资源
    最近更新 更多