shadab
shadab

Reputation: 207

Jaxb Unmarshalling an xml document anytype

I have a service method which has a method with 2 params :

    public int ReadXmlDocAsString(@WebParam(name = "password") String password, 
                                  @WebParam(name = "doc") com.vincari.hl7.jaxws.xmlDoc<Object> doc){

    }

My xmlDoc class is as follows :

public class xmlDoc<T> {
@XmlMixed
@XmlAnyElement(lax = true)
protected List<T> content;
public List<T> getContent() {
    if (content == null) {
        content = new ArrayList<T>();
    }
    return this.content;
}
public void setContent() {
    content = this.content;
}   
}

and my sample soap request is :

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:sample="http://sample.vincari.com/">
<soap:Header/>
<soap:Body>
      <sample:ReadXmlDocAsString>
         <!--Optional:-->
         <password>1234</password>
         <!--Zero or more repetitions:-->
         <doc>
         <Header Application="SourceApp">
         <CustomerData>
         <Customer AccountNumber="1234" customername="ABCD">
         </Customer>
         <CustomerData>
         </Header>
         </doc>
      </sample:InsertPatientInfoImpl>
   </soap:Body>
</soap:Envelope>

I am able to deploy the webservice using cxf. But when I try to map the doc object and try to get it as string. I used toString and tried to transform using DOMSource as well. How can I convert this to a string. Any help much appreciated.

Upvotes: 1

Views: 697

Answers (1)

shadab
shadab

Reputation: 207

Document inputDoc = ((Element) doc.getContent().get(0)).getOwnerDocument();
String inputPayload = "";
StringWriter sw = new StringWriter();
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(new DOMSource(inputDoc), new StreamResult(sw));
            inputPayload = sw.toString();
            System.out.println("Read the input stream successfully "+ inputPayload);

When you map using anytype in the wsdl parameter. It will map to a list or other collection and then we need to use DOM to map it to a string.

Upvotes: 1

Related Questions