问题描述
尝试将我的soap XML解组为JAXB对象时出现以下错误。
while trying to unmarshall my soap XML to JAXB object am getting the following error.
我们收到的错误是期望元素为none。在解组SOAP XML时应该做什么具体的事情。
We are getting error that expected element is none. Should anything specific be done while unmarshalling the SOAP XML.
javax.xml.bind.JAXBContext jaxbContext = (javax.xml.bind.JAXBContext) JAXBContext.newInstance(Class.forName(requestName));
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(SoapXmlString);
reqInfo = unmarshaller.unmarshal(reader);
我收到以下错误:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are (none)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:254)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:249)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:116)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement
这里是示例XML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v2="http://example.com/v2">
<soapenv:Header/>
<soapenv:Body>
<v2:createSession>
<v2:client>
<!--Optional:-->
<v2:name>?</v2:name>
<!--Optional:-->
<v2:clientId>?</v2:clientId>
<!--Optional:-->
<v2:requestId>?</v2:requestId>
</v2:client>
<!--Optional:-->
<v2:oldSessionId>?</v2:oldSessionId>
<!--Optional:-->
<v2:clientIp>?</v2:clientIp>
<!--Optional:-->
<v2:clientIpStatus>?</v2:clientIpStatus>
<!--Optional:-->
<v2:superBYOBFlow>?</v2:superBYOBFlow>
<!--Optional:-->
<v2:FlowParams>?</v2:FlowParams>
<!--Optional:-->
<v2:deviceInfo>?</v2:deviceInfo>
</v2:createSession>
</soapenv:Body>
</soapenv:Envelope>
请帮忙。
推荐答案
我认为你不会考虑SOAP信封...你生成的JAXB Unmarshaller不会知道关于Body或Envelope标签的任何内容,它都会期望你的createSession成为根元素,因此出现意外元素错误。
I don't think you're taking the SOAP envelope into account... Your generated JAXB Unmarshaller won't know anything about the Body or Envelope tags, it will be expecting your createSession to be the root element hence the "unexpected element" error.
你需要从中提取内容首先是信封,你可以使用message.getSOAPBody()。extractContentAsDocument(),如果你先从你的内容创建一个SOAPMessage对象。
You need to extract the content from the Envelope first, you can do this with message.getSOAPBody().extractContentAsDocument() if you create a SOAPMessage object from your content first.
这是非常繁琐的,这里是我的中的一个工作示例
It's quite fiddly to do, here's a working example from my blog
String example =
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header /><soapenv:Body><ns2:farm xmlns:ns2=\"http://adamish.com/example/farm\"><horse height=\"123\" name=\"glue factory\"/></ns2:farm></soapenv:Body></soapenv:Envelope>";
SOAPMessage message = MessageFactory.newInstance().createMessage(null,
new ByteArrayInputStream(example.getBytes()));
Unmarshaller unmarshaller = JAXBContext.newInstance(Farm.class).createUnmarshaller();
Farm farm = (Farm)unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
如果您没有在架构.xsd文件中声明您的命名空间,那么您将会看看你有的错误。
It seems that if you don't declare your namespace in your schema .xsd file then you'll see the error you have.
我创建了一个带有根元素createSession的虚拟模式,并通过添加targetNamespace属性并重新生成JAXB类,错误消失了
I created a dummy schema with a root element createSession and by adding the targetNamespace attribute and regenerating the JAXB classes the error disappeared
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/v2"> <!-- targetNamespace essential for JAXB to work-->
<xs:element name="createSession">
<xs:complexType>
<xs:attribute name="foo" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
这篇关于如何将SOAP XML解组为Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!