Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmarshall SOAP Message using JAXB

when i unrmarshall below SOAP message, i am getting exception. Can someone tell me what is wrong?

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:hios="http://schemas.datacontract.org/2004/07/HIOSCommonObjects">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:ValidIssuersProducts>
         <!--Optional:-->
         <tem:li>
            <!--Zero or more repetitions:-->
            <hios:IssuerProductRequest>
               <!--Optional:-->
               <hios:Issuer>33104</hios:Issuer>
               <!--Optional:-->
               <hios:Product>33104VA001</hios:Product>
            </hios:IssuerProductRequest>
         </tem:li>
      </tem:ValidIssuersProducts>
   </soapenv:Body>
</soapenv:Envelope>

Unmarshalling code is as below, clazz is "fully qualified name of ValidIssuersProducts" request.

public static <T> Object unmarshall(String xml, String clazz) throws ClassNotFoundException, JAXBException {
        JAXBContext context = JAXBContext.newInstance(Class.forName(clazz));
        ByteArrayInputStream input = new ByteArrayInputStream (xml.getBytes());     
        Unmarshaller unmarshaller = context.createUnmarshaller();   
        //JAXBElement<ValidIssuersProducts> obj = (JAXBElement<ValidIssuersProducts>) unmarshaller.unmarshal(new StreamSource(input), ValidIssuersProducts.class);

        ValidIssuersProducts value = (ValidIssuersProducts)unmarshaller.unmarshal(input);
        return  value;

    }

I am getting below exception:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are <{http://tempuri.org/}ArrayOfIssuerProductRequest>,<{http://tempuri.org/}ArrayOfIssuerProductResponse>,<{http://tempuri.org/}IssuerProductRequest>,<{http://tempuri.org/}IssuerProductResponse>,<{http://tempuri.org/}ValidIssuersProducts>,<{http://tempuri.org/}ValidIssuersProductsResponse>
    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(UnmarshallingContext.java:1049)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:478)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:459)
    at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:71)
    at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:148)
    at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:239)
    at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:122)
like image 441
Chintan Avatar asked Apr 30 '13 02:04

Chintan


People also ask

How to unmarshal SOAP XML to Java object?

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.

What is marshalling and unmarshalling in JAXB?

JAXB definitionsMarshalling is the process of transforming Java objects into XML documents. Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API. It provides API for marshalling, unmarshalling and validating.

What is JAXB Unmarshal?

The JAXB Unmarshaller interface is responsible for governing the process of deserializing the XML data to Java Objects. The unmarshalling to objects can be done to variety of input sources.


1 Answers

You need to unmarshal from the nested element ValidIssuersProducts which corresponds to your mapped content instead of the SOAP Envelope element. You can use a StAX XMLStreamReader to parse the XML and advance to the correct element and then unmarshal from the `XMLStreamReader at that point.

For More Information

  • http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html
like image 106
bdoughan Avatar answered Sep 19 '22 21:09

bdoughan