WSO2Con 2013 CFP Banner

How do I Convert a Javabean into an OMElement in Axis2?

Archived Content
This article is provided for historical perspective only, and may not reflect current conditions. Please refer to relevant product page for more up-to-date product information and resources.
Discuss this article on Stack Overflow
By Deepal Jayasingha
  • 5 Dec, 2006
  • Level: 
  • Reads: 13155
As you know XML representation in Apache Axis2 is AXIOM (AXis Object Model). Therefore, if you want to send or receive messages, then you have to use AXIOM.
Deepal Jayasingha

WSO2 Inc.
As an example, say that we want to invoke a service and we do not like to create an OMElement for the request payload. And further assume that we have Java bean object and we want to create request payload using that. Is this possible? Yes, of course. Case 1: We want to create OMElement from one Java be an object, and assume that class name of our bean object is "Person". Then using following lines of code we can create OMElement for our bean object: Person man = new Person();person.setName("I am Axis2");XMLStreamReader reader = BeanUtil.getPullParser(man);StreamWrapper parser = new StreamWrapper(reader);StAXOMBuilder stAXOMBuilder =OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), parser);OMElement element = stAXOMBuilder.getDocumentElement(); Case 2: Now assume we want to create an OMElement for an Object array. Use the following lines of code to do that: OMElement omElement = BeanUtil.getOMElement(warapperElementName, argsArray, null, false, null); wrapperElementName : Qname of the root element you want to create. Then all the objects will be serialize and added as child elements. ArgsArray : Object array that you want to serialize. Applies To: Apache Axis2/Java 1.0 or later