Vadim Berman
Vadim Berman

Reputation: 2032

Passing a raw XmlElement to Apache CXF client from WCF (basicHttpBinding)

A WCF service returns System.Xml.XmlElement in one method. It all works perfectly well with .NET clients.

However, I am not sure how to obtain the value from the Apache CXF-generated client. The return type is MyMethodNameResult, and the only meaningful methods are getAny and setAny. Not sure what to do with them.

Can I get a string or a stream to build an XML DOM from?

Upvotes: 0

Views: 280

Answers (1)

user1407172
user1407172

Reputation: 36

In my case cxf generates objects like

public static class MyObject {
   @XmlMixed
   @XmlAnyElement(lax = true)
   protected List<Object> content;

   public List<Object> getContent() {
      if (content == null) {
          content = new ArrayList<Object>();
      }
      return this.content;
    }
}

Then I use this function to get an xml string

private static String elementToString(final ElementNSImpl doc) {

    try {
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        transformer.transform(new DOMSource(doc.getOwnerDocument()), new StreamResult(sw));
        return sw.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Error converting to String", ex);
    }
}

You call it like

elementToString((ElementNSImpl) myObject.getContent().get(0));

Upvotes: 2

Related Questions