gpt4 book ai didi

namespaces - XML(带命名空间)到对象解码

转载 作者:行者123 更新时间:2023-12-03 16:51:08 25 4
gpt4 key购买 nike

我从 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 到对象)解码它。

最佳答案

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

命名空间

您应该使用 @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 的解码方法之一指定要解码到的类。范围:
    return unmarshaller.unmarshal(xml, QueryByLNResponse.class)

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


  • 性能

    在相同的代码中,您正在创建一个新的 JAXBContext每次你做一个解码。 JAXBContext是一个线程安全对象,可以创建一次并重用以提高性能。

    关于namespaces - XML(带命名空间)到对象解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11843142/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com