2014-09-29 28 views
10

durante il tentativo di annullare l'operazione il mio soap XML all'oggetto JAXB viene visualizzato il seguente errore.Come unmarshall XML SOAP in oggetto Java

Riceviamo l'errore che l'elemento previsto non è nessuno. Dovrebbe essere fatto qualcosa di specifico durante lo smantellamento del 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); 

sto ottenendo il seguente errore:

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 

ed ecco il XML di esempio

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

Si prega di aiuto.

+0

Quale valore 'requestName' hanno? – Priyesh

+0

Vedere [qui] (http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html). –

+0

@priyesh: è un nome di classe. Es: createSessionRequest – srinath

risposta

17

Non penso che tu stia prendendo in considerazione la busta SOAP ... Il tuo JAXB Unmarshaller generato non saprà nulla dei tag Body o Envelope, si aspetterà che il tuo createSession sia l'elemento principale da cui errore "elemento inatteso".

È necessario prima estrarre il contenuto dalla busta, è possibile farlo con message.getSOAPBody(). ExtractContentAsDocument() se si crea prima un oggetto SOAPMessage dal contenuto.

E 'abbastanza complicato da fare, ecco un esempio di lavoro dalla mia 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()); 

Sembra che se non si dichiara lo spazio dei nomi nello schema XSD file di poi vedrete l'errore di avere .

ho creato uno schema fittizio con un createSession elemento radice e aggiungendo l'attributo targetNamespace e rigenerando le classi JAXB l'errore è scomparso

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

Grazie per l'aiuto. Ho provato lo stesso codice sostituendo la classe Farm con la classe CreateSessionRequest. Ma sto seguendo nuovamente l'errore. javax.xml.bind.UnmarshalException: elemento inatteso (uri: "http://example.com/v2", locale: "createSession"). Gli elementi previsti sono (nessuno) – srinath

+0

@srinath Puoi pubblicare il tuo file .xsd e daremo un'occhiata più da vicino. – Adam