Axiom
Axis Object Model (axiom) is an XML object model designed to improve memory usage and performance during XML processing, based on pull parsing.
Pull Parsing is based on Stax (streaming API for XML). Pull Parsing is a recent trend in XML processing, while Sax and Dom are both push-based parsing methods, that is to say, the parsing is controlled in the parser itself. The push parsing method is easy to use, but the processing efficiency of XML documents is not good (because the object model generated in the memory ). The pull Parsing Method reverses this control method and enhances parser, which can be processed only when the user needs it. The user decides to process or ignore the events generated by 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 the user's needs. The following example describes the concept: Assume that a user needs to obtain the <location> element value of a person from the XML input stream, the object model built by Axiom will always contain the content ending with the <location> 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> femal </sex>
<Location> colomo, Sri Lanka </location>
<-- Object model is only created on top of this -->
</Person>
<Person>
<Name> dihini himahansi </Name>
<Sex> femal </sex>
<Location> colomo, Sri Lanka </location>
</Person>
</Persons>
The advantage here is that you try to use only the memory that meets your 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 of axis2 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 use convenient methods such as envelop. getheaders and envelope. getbody to view soap.
Axis2 maps custom objects, arrays, and lists in parameters or returned values of the web service method to the omelement type, this is the conversion method of omelement between custom types. Below we provide 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 ("15900000000 ");
Javax. xml. Stream. xmlstreamreader reader = beanutil. getpullparser (man );
Streamwrapper parser = new streamwrapper (Reader );
Staxombuilder = omxmlbuilderfactory. createstaxombuilder (omabstracloud. 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 );
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;
}
Iterrator 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 (omelement. 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 );
}
}
}
}
Http://www.blogjava.net/juleven/archive/2006/12/08/86458.html
Axiom read XML:
// First build parser,
Xmlstreamreader parser = xmlinputfactory. newinstance (). createxmlstreamreader (
New fileinputstream ("5.xml "));
// The Builder object is also required,
Staxombuilder builder = new staxombuilder (parser );
// Get the root element
// Omelement documentelement = builder. getdocumentelement ();
Omdocument Doc = builder. getdocument ();
Omelement CRE = Doc. getomdocumentelement (). getfirstchildwithname (New QNAME ("fool "));
// Omelement has a series of get methods to obtain the content.
Re. serialize (system. Out); // cache on
Re. serializeandconsume (system. Out); // cache off
// Will not build the omtree in the memory.
// So you are at your own risk of losing information.
String crestr = cre. tostringwithconsume ();
// Call tostring, will build the omtree in the memory.
System. Out. println (CRES );
Axiom write XML:
// Writer can be built as the output device,
Xmlstreamwriter writer = xmloutputfactory. newinstance (). createxmlstreamwriter (
New fileoutputstream ("2.xml "));
// Generally, the element in the XML document is constructed through omfactory. The following is some sample code.
Omfactory factory = omdomainactfactory. getomfactory ();
Omdocument Doc = factory. createomdocument ();
Omnamespace NS = factory. createomnamespace ("http://demo.axiom", "x ");
Omnamespace NS1 = factory. createomnamespace ("http://ot.demo.axiom", "Y ");
Omelement root = factory. createomelement ("root", NS );
Omelement elt11 = factory. createomelement ("fool", NS1 );
Elt11.addchild (factory. createomtext ("YY "));
Omelement ele = factory. createomelement ("ele", "http: // namespace", "ns ");
Ele. addchild (factory. createomtext ("ele "));
Root. addattribute (factory. createomattribute ("ATTR", NS, "test ATTR "));
Root. addchild (elt11 );
Root. addchild (Ele );
Doc. addchild (Root );
Root. serialize (writer); // cache on
Writer. Flush ();
Doc. serializeandconsume (New fileoutputstream ("3.xml "));
Omoutputformat oOf = new omoutputformat ();
Doc. serializeandconsume (New fileoutputstream ("5.xml"), oof); // cache off // ELE. Detach ();
Ele. serialize (system. Out); // even if detach (), the output of ELE.
Doc. serialize (system. Out); // If detach () is used, no ELE is included in the document.
For serialize and serializeandconsume, the former will force the construction of omtree, or the former will not.
With regard to detach, it only affects the relationship between omelement itself and omtree, and does not affect omelement itself.
There is also a build method corresponding to it. The build will force the entire omtree to be built.
These two methods are usually used to process the relationship between omelement and omtree. Creates an omelement (build) from the input stream and detach the omelement from the input stream for the output stream. The input stream and output stream are different omtree.
XML document for testing (5.xml ),
<? XML version = '1. 0' encoding = 'utf-8'?>
<X: Root xmlns: x = "http://demo.axiom" X: ATTR = "test ATTR">
<Y: Fool xmlns: Y = "http://ot.demo.axiom"> YY </Y: Fool>
<Ns: ele xmlns: NS = "http: // namespace"> ele </ns: ele>
</X: root>
Refer:
Axiom Tutorial: http://ws.apache.org/commons/axiom/OMTutorial.html