2014-09-29 86 views
10

尝试解除我的SOAP XML到JAXB对象时出现以下错误。如何将SOAP XML解组为Java对象

我们得到错误,预期的元素是没有的。在解编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> 

请你帮忙。

+0

是“requestName”有什么价值? – Priyesh 2014-09-29 10:47:27

+0

请参阅[这里](http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html)。 – 2014-09-29 10:48:43

+0

@priyesh:它的类名。例如:createSessionRequest – srinath 2014-09-29 10:50:52

回答

17

我不认为你在考虑SOAP信封......你生成的JAXB Unmarshaller不会知道任何关于Body或Envelope标签的信息,它会期待你的createSession成为根元素,因此“意外元素”错误。

您需要首先从信封中提取内容,如果您先从内容创建SOAPMessage对象,则可以使用message.getSOAPBody()。extractContentAsDocument()方法执行此操作。

这是相当繁琐的事,这里是从我的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文件命名空间,然后你会看到你有错误。

我创建了一个根元素了createSession,并通过添加targetNamespace属性和再生的JAXB类的虚拟架构中的错误消失

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

感谢您的帮助..我通过用CreateSessionRequest类替换Farm类来尝试相同的代码。但我再次遇到错误。 javax.xml.bind.UnmarshalException:意外元素(uri:“http://example.com/v2”,local:“createSession”)。预期的元素是(无) – srinath 2014-09-29 11:12:34

+0

@srinath你可以发布你的.xsd文件,我会仔细看看。 – Adam 2014-09-29 12:24:47