Javax.xml.bind.UnmarshalException: unexpected element (uri: “”, local:”soap:Envelope”)

javajaxbweb serviceswsdl

Hi I have generated java classes from a WSDL using wsimport.But I have written the response to a file *.xml. But now I want to read this xml file and populate the java classes that already have been generated.

I have tried:

JAXBContext jc = JAXBContext.newInstance(Report.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Report rc = (Report) unmarshaller.unmarshal(source);

or

JAXBContext jc = JAXBContext.newInstance(Report.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Report rc = (Report) unmarshaller.unmarshal(new File("file.xml"));

Report is the class that I get as a response when I send the request

In the first case I get

javax.xml.bind.UnmarshalException: unexpected element (uri: "", local:"soap:Envelope") Expected elements are: (<{"http://pagewhereisthewsdl.com"}CLASSES>)+

In the second case

javax.xml.bind.UnmarshalException: unexpected element (uri: "http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope") Expected elements are: (<{"http://pagewhereisthewsdl.com"}CLASSES>)+

XML is like this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
              <ns3:GetReportOnlineResponse xmlns:ns2="http://pagewhereisthewsdl.com/document" xmlns:ns3="http://pagewhereisthewsdl.com/endpoint">
                 <ns2:Report>
                       ...
                 </ns2:Report>
           </ns3:GetReporteOnlineResponse>
       </soap:Body>
</soap:Envelope>

Or what can I do ?

Best Answer

I believe you're not taking the SOAP envelope into account. You need to extract the body's content first.

String xml = "<INSERT XML>";
SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(xml.getBytes()));
JAXBContext jc = JAXBContext.newInstance(Report.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Report rc = (Report) unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
Related Topic