How can I create and Apache Axiom element from a E4X object from a script running in Rhino?
I am running a script running in Rhino, which creates an E4X object like this:
var s = <product id="123">
<name>Google Search</name>
<source>https://google.com</source>
</product>
I want to include such XML in a SOAP mess开发者_运维知识库age. I am using Apache Axis 2 ServiceClient for creating soap message. What I am looking for is a way to convert the E4X xml object into an Apache AXIOM element so that it can be added to SOAP message via a call to:
ServiceClient.addHeader(org.apache.axiom.om.OMElement omElement)
So far it appears that converting E4X object to String and then using StAXOMBuilder (or one of it's subclasses) is the simplest option.
XMLInputFactory xif= XMLInputFactory.newInstance();
XMLStreamReader reader= xif.createXMLStreamReader(new StringReader(stringFromRhinoE4X));
StAXOMBuilder builder=new StAXOMBuilder(reader);
OMElement header= builder.getDocumentElement();
Edit: Although the above code works, the resulting OMElement cannot be passed to ServiceClient.addHeader(org.apache.axiom.om.OMElement omElement). Following code can be used to create an OMElement representing SOAPHeader:
CharArrayDataSource arrayDataSource = new CharArrayDataSource(contentXML.toCharArray());
SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
SOAPHeaderBlock soapHeader = factory.createSOAPHeaderBlock(headerName, factory.createOMNamespace(namespace, nameSpacePrefix), arrayDataSource);
service.addHeader(soapHeader);
精彩评论