Conversion between omelement and Java objects in axis2

Source: Internet
Author: User

I recently used axis2 to deploy a web service. I don't want to talk about how to deploy a web service using axis2. There are many related articles on the website, here, I just want to talk about how to convert an axiom object to an object in Java.

The following section describes the axiom:

Axiom

Axis Object Model (axiom) is an XML object model designed to improve memory usage and performance during XML processing, based on pull parsing. By using the streaming API for XML (Stax) Pull parser, axiom (also known as om) can control the parsing process to provide support for delayed building. Delayed building means that axiom does not fully construct the object model, and the rest of the model is built based on user requirements. The following example describes the concept:

Assume that a user needs to obtain the first person's<Location>Element value. The object model built by Axiom will always include<Location>The End Content of the element, and keep other content in the stream:

Listing 1. Building the Axiom part of the Object Model

<Persons>  <Person>     <Name>Dihini Himahansi</Name>     <Sex>Female</Sex>     <Location>Colombo, Sri Lanka</Location>       <--- Object model is being built only up to this point  </Person>  <Person>     <Name>Thushari Damayanthi</Name>     <Sex>Female</Sex>     <Location>Elpitiya, Sri Lanka</Location>  </Person></Persons>

The advantage here is that we try to use only the memory that can meet the user's needs. If you want to access several or thousands of bytes before a large document, the delay building function will improve the memory requirements of the application.

The Stax Event can be obtained from any element, regardless of whether the object model is fully constructed. In some cases, this feature in Axis 2 is very useful. For example, when axis2 is transmitted as an intermediary, if you only need to read the header of the SOAP message, Axiom will prevent it from reading the entire SOAP message, so that it has high memory efficiency. Another example is that when the web service implementation can directly use the Stax event, because axiom is used, the memory required by the Web Service is very small.

In addition, axiom has built-in message transmission optimization mechanism (MTOM) support. For the Axiom architecture, You can implement the Axiom interface and insert it into axis2 to execute your own object model.

Since axiom was initially developed as an object model of axis2, axiom provides a soap interface built on the basic axiom API. This allows you to useenvelope.getHeadersAndenvelope.getBodyAnd other convenient methods to view soap.

Axis2 maps custom objects, arrays, and lists in parameters or returned values of the web service method to the omelement type, in this case, we need to compile the conversion method between omelement and custom type quality control. Below are several common methods:

Example 1: Use the following method to generate an omelement for a custom object:

Person man = new person ();
Man. setname ("warlaze ");
Man. setage (25 );
Man. setaddress ("Bei Jing ");
Man. setphonenum ("13900000000 ");
Javax. xml. Stream. xmlstreamreader reader = beanutil. getpullparser (man );
Streamwrapper parser = new streamwrapper (Reader );
Staxombuilder = omxmlbuilderfactory. createstaxombuilder (omw.actfactory. getomfactory (), Parser );
Omelement element = staxombuilder. getdocumentelement ();

Case 2: The omelement generation method of the list or array type:

List <person> List = new arraylist <person> ();
List. Add (man );
List. Add (man );
Omelement = beanutil. getomelement (New QNAME ("root"), list
. Toarray (), new QNAME ("person"), false, null );

Case3: Method for parsing the omelement generated by a list or array containing the basic type:

Private Static list <string> getresults (omelement element ){
If (element = NULL ){
Return NULL;
}
Iterator = element. getchildelements ();
List <string> List = new arraylist <string> ();
While (iterator. hasnext ()){
Omnode = (omnode) iterator. Next ();
If (omnode. GetType () = omnode. element_node ){
Omelement = (omelement) omnode;
If (omelement. getlocalname (). Equals ("string ")){
String temp = omelement. gettext (). Trim ();
System. Out. println (temp );
List. Add (temp );
}
}
}
Return list;
}

Case4: Method for parsing a list or array containing a custom Java type:

Private Static list <person> getresults (omelement element) throws axisfault {
If (element = NULL ){
Return NULL;
}
Iterator = element. getchildelements ();
List <person> List = new arraylist <person> ();
While (iterator. hasnext ()){
Omnode = (omnode) iterator. Next ();
If (omnode. GetType () = omnode. element_node ){
Omelement = (omelement) omnode;
If (omelement. getlocalname (). tolowercase (). Equals ("person ")){
Person = (person) beanutil. processobject (omelement,
Person. Class, null, true,
New defaultobjectsupplier ());
List. Add (person );
}
}
}
Return list;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.