AXIOM detailed principles

Source: Internet
Author: User
Tags addchild

Turn from: http://warlaze.blog.sohu.com/58477971.html

AXIOM

The Axis object Model (AXIOM) is an XML object model designed to improve memory utilization and performance during XML processing, based on pull parsing.

Pull parsing is based on Stax (streaming API for XML), and pull parsing is a recent trend in XML processing, and sax and Dom are based on push parsing, meaning that parsing is controlled in the parser itself. The push parsing method is easy to use, but is inefficient when working with XML documents (because the completed object model is generated in memory). Pull parsing reverses this control and enhances the parser, which is handled only when the user needs it. The user decides to process or ignore events generated by parser.

AXIOM (also known as OM) can control the parsing process to provide deferred build support. Deferred build refers to axiom not fully building the object model, and the rest of the model is built on the needs of the user. The following example illustrates this concept: assuming that a user needs to derive a person's <Location> element value from the XML input stream, the Axiom constructed object model will always contain the content to the end of the <Location> element, leaving the other content in the stream:

Listing 1: Axiom Partial build of the object model

<Persons>
<Person>
<name>dihini himahansi</name>
<Sex>Femal</Sex>
<location>colomo,sri lanka</location>
The <--object model is only built on top of this-
</Person>
<Person>
<name>dihini himahansi</name>
<Sex>Femal</Sex>
<location>colomo,sri lanka</location>
</Person>
</Persons>

The advantage here is to use as little memory as possible to satisfy the user's needs. If a user wants to access the first few bytes or thousands of bytes in a larger document, the deferred build feature will improve the memory requirements of the application.

Stax events can be obtained from any element, regardless of whether the object model is completely built. In some cases, this feature of Axis2 is very useful. For example, when AXIS2 is passed as a mediator, only the header,axiom that need to read the SOAP message will prevent it from reading the entire SOAP message, making it highly memory efficient. Another example is when the Web service implementation is able to use the Stax event directly, because the memory required to take the Axiom,web service is very small.

In addition, the axiom has built-in message transfer optimization mechanism (MSG Transfer optimization mechanism,mtom) support. For the axiom architecture, you can execute your own object model by implementing the Axiom interface and inserting it into AXIS2.

Since Axiom was originally developed as an object model for AXIS2, Axiom provides a SOAP interface built on top of the underlying Axiom API. This allows you to view soap using a convenient method such as Envelop.getheaders and Envelope.getbody.

AXIS2 maps the custom objects, arrays, lists, and the like in the Web service method to the Omelement type, which is what we need to write omelement the conversion method between the custom types, and here are a few common methods:

CASE1: Custom Object Generation Omelement method:

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 staxombuilder=omxmlbuilderfactory. Createstaxombuilder (omabstractory. Getomfactory (), parser);
Omelement element=staxombuilder. Getdocumentelement ();

case2:list or array type generation Omelement Method:

List<person> list=new arraylist<person> ();
List. Add (man);
Omelement omelement=beanutil. Getomelement (New QName ("root"), List.toarray (), New QName ("person"), false,null);

CASE3: Resolves a method that contains the base type of a list or array-generated omelement:

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= (Omnode) iterator.next ();
if (Omnode.gettype () ==omnode.element_node) {
Omelement omelement= (omelement) Omnode;
if (Omelement.getlocalname (). Equals ("string")) {
String temp=omelement.gettext (). Trim ();
SYSTEM.OUT.PRINTLN (temp);
List.add (temp);
}
}
}
return list;
}

CASE4: Resolves a method that contains a list or array of custom Java types:

private static list<person> GetResults (omelement element) throws axisfault{
if (element==null) {
return null;
}
Iterator iterator=element.getchildelements ();
List<person> list=new arraylist<person> ();
while (Iterator.hasnext ()) {
Omnode omnode= (Omnode) iterator.next ();
if (Omelement.gettype () ==omnode.element_node) {
Omelement omelement= (omelement) Omnode;
if (Omelement.getlocalname (). toLowerCase (). Equals ("person")) {
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:

//Build parser first,
Xmlstreamreader parser = Xmlinputfactory.newinstance (). Createxmlstreamreader (
New FileInputStream ("5.xml"));
Builder objects are 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 get the content.

Cre.serialize (System.out); Cache on
Cre.serializeandconsume (System.out); Cache off

Won't build the Omtree in the memory.
So is at your own risk of losing information.
String crestr = Cre.tostringwithconsume ();
Call ToString, would build the Omtree in the memory.
System.out.println (CRE);

Axiom Write xml:

//Can build writer to do the output,
Xmlstreamwriter writer = Xmloutputfactory.newinstance (). Createxmlstreamwriter (
New FileOutputStream ("2.xml"));

You typically construct an element in an XML document through Omfactory, and here are some sample code.
Omfactory factory = Omabstractfactory.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 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 Detach () will still output ele
Doc.serialize (System.out);//If Detach (), There will be no ele in the document.


With regard to serialize and Serializeandconsume, the former will force the construction of Omtree, or not.
Regarding detach, it only affects the relationship between Omelement itself and omtree, and does not affect the omelement itself.
There is also a build method corresponding to the build that forces the build to omtree out.
These two methods are usually used to deal with the relationship between Omelement and Omtree. Build the Omelement (build) from the input stream and disconnect the omelement from the input stream (detach) so that it can be placed in the output stream. The input stream and the output stream are different omtree.


The XML document for the test (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>

Reference:
AXIOM Tutorial: http://ws.apache.org/commons/axiom/OMTutorial.html

AXIOM detailed principles

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.