Sunday, July 28, 2013

Java : (AXis Object Model)

(AXis Object Model)

Refers to the information included inside the XML.
DOM and JDOM are XML models which are made up of objects, AXIOM is similar to them.

AXIOM is based on ‘Pull Parsing’. Unlike ‘push based’ XML processing frameworks (SAX and DOM), which were not efficient in terms of handling large XML documents since these framework generates complete memory model in memory. Pull parsing gives control to user, here user gives command and he/she can decide to store or discard events generated from parser. AXIOM uses StAX for pull parsing.

AXIOM is lightweight.
AXIOM does not create objects unless a need arise for them, this is called ‘Deferred building’.
AXIOM defines below interfaces to represent nodes in XML :

Interface / Class
Remark
OMElement
Represents an elemet infuset information.
 
OMText
Represents character data that appears inside an element.
 
OMComment         
For Comments
 
OMDocType
The doc type

OMProcessingInstruction

 
Processing instructions with target to process and value.
OMSourcedElement
Element whose data is backed by an arbitrary Java object.

AXIOM api can be implements in two ways :
1)LLOM : Linked List implementation uses linked lists to store collection of nodes.
2)DOOM (DOM over OM ) supports DOM api.

Default implementation uses factory design pattern. For each of above implementation, there are 3 factories.

For example , if we need factory to process plain XML, we’ll use OMAbstractFactory api. Here is a code snap:

// creates factory to process plain XML
OMFactory plainXMLProcessFactory = OMAbstractFactory.getOMFactory();
// Namespace can be created through createOMNamespace, It takes 2 parameters URI //and Prefix for that URI
OMNamespace nameSpace1 = plainXMLProcessFactory.createOMNamespace(“uri1”,"prefix1");
OMNamespace nameSpace2 = plainXMLProcessFactory.createOMNamespace("uri2","prefix2”);
// xmlns:prefix1=”uri1”
// Elements can be created using ‘createOMElement’ method, this is a //overloaded method
OMElement element = plainXMLProcessFactory.createOMElement("foo",namespace1);

// foo is localname here
// To create text for node AXIOM provides createOMText method

OMText text = plainXMLProcessFactory.createOMText(parentOMContainer, srcOMText);

To add child in elements OMElement provides addChild method , similarly to add attributes addAttribute method helps . Node can be removed by calling detach method on it.

Here are the steps to create an object model from an input stream
 
1)    Create InputStream object
     InputStream inStream = new FileInputStream(xmlFile);
2)    Create OMBuilder instance 
OMXMLParserWrapper builder = 
        OMXMLBuilderFactory.createOMBuilder(in);
3)    Get root elements
     OMElement documentElement = builder.getDocumentElement();
 
 
 
 

AXIOM uses “builder” to build the XML object model in memory. This builder builds object only when the relevant information is absolutely required.
AXIOM comes bundled with several builders:

  • StAXOMBuilder: This will build a generic memory model from any XML input source, such as a file, string, stream, etc.
  • StAXSOAPModelBuilder: This will build an object structure of SOAP XML in memory, which can be accessed using an "SOAPish" API. For example, when using it, you get a SOAPEnvelope class, with which you can call methods like getHeaders() andgetBody(). But this API is still an extension of the generic AXIOM API. This is the model mainly used within the Axis2 project.
  • MTOMBuilder: This can be regarded as the first implementation of MTOM, the new API for sending attachments using some optimization algorithms. The latest AXIOM sources have full support for MTOM.

Please note that the current AXIOM implementation lags support for processing instructions and DTD information items of the XML infoset. But there is an ongoing effort within the Axis2 team to provide these features as well.

Writing XML with AXIOM :

Here is a sample to create XML and write it to console :

package com.rtvk;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;

public class AXIOM_Sample {
public static void main(String[] args){
//The factory to process XML
OMFactory plainXMLProcessFactory = OMAbstractFactory.getOMFactory();

// namespace
OMNamespace ns =
plainXMLProcessFactory .createOMNamespace("http://rtvk.com","rp");


//root element
OMElement rootElement =
plainXMLProcessFactory .createOMElement("rootElement", ns);

// child 1
OMElement firstElement =
plainXMLProcessFactory .createOMElement("firstElement", ns);
firstElement.setText("I am First");
firstElement.addAttribute("attrFoFirstEle", "valForFirstAttr", ns);


// child 2
OMElement secondElement =
plainXMLProcessFactory .createOMElement("secondElement", ns);
secondElement.setText("I am second");
secondElement.addAttribute("attrFoSecondEle", "valForSecondAttr", ns);

//adding to root
rootElement.addChild(firstElement);
rootElement.addChild(secondElement);

//to write
XMLStreamWriter writer;
try {

//write to console
writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
rootElement.serialize(writer, false);
writer.flush();
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (FactoryConfigurationError e) {
e.printStackTrace();
}
}
}

Output in console :

<rp:rootElement xmlns:rp="http://rtvk.com">
<rp:firstElement rp:attrFoFirstEle="valForFirstAttr">
I am First
</rp:firstElement>
<rp:secondElement rp:attrFoSecondEle="valForSecondAttr">
I am second
</rp:secondElement>
</rp:rootElement>


READING / Navigate / Travels XML With AXIOM :

package com.rtvk.services;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
public class AXIOM_Sample_Read {
public static void main(String[] args){
FileReader fileReader;
try {
fileReader = new FileReader("Sample.xml");
XMLStreamReader streamReader = XMLInputFactory.newInstance().createXMLStreamReader(fileReader);

StAXOMBuilder builder = new StAXOMBuilder(
OMAbstractFactory.getOMFactory(),
streamReader);

OMElement documentElement = builder.getDocumentElement();

//complete XML
System.out.println(builder.getDocument().getOMDocumentElement());

// Child node text
Iterator<OMElement> iterator = documentElement.getChildElements();

while(iterator.hasNext()){
OMElement omElement = (OMElement)iterator.next();
System.out.println(omElement.getText());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (FactoryConfigurationError e) {
e.printStackTrace();
}
}
}

Output in Console :
<rp:rootElement xmlns:rp="http://rtvk.com"><rp:firstElement rp:attrFoFirstEle="valForFirstAttr">I am First</rp:firstElement><rp:secondElement rp:attrFoSecondEle="valForSecondAttr">I am Second</rp:secondElement></rp:rootElement>
I am First
I am second
Courtesy: Ritvik Pandya (rtvk05@gmail.com )

No comments:

Post a Comment