Introduction to AXIOM

 Today I am going to discuss the basic concepts of AXIOM. First of all we will look at what AXIOM is.

What is AXIOM?

AXIOM stands for AXIs2 Object Model. This is the XML object model used  by Apache AXIS2 web services.

What is the difference from other models?

For processing XML, we usually use Object Models (OM). There are a lot of different kinds of  them to use with Java. AXIS2 uses AXIOM to process XML documents. AXIOM uses pull parsing method (with StAX standard API), while others using push parsing. In pull parsing, the user can handle the process and can control which events to store or delete.

p2

Let’s start the journey…

Hope you have read my post on ‘XML basics’. Let’s use the same XML document for this purpose.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


    
        D6563
        Xperia Z2a
        Sony
        2014
        
            137mm
            72mm
            11mm
        
        163
    

    
        VS876
        Lucid 3
        LG
        2013
        
            131.6mm
            66mm
            9.9mm
        
        123.9
    
  • Start creating a maven project (I hope you are familiar with maven) and create the above XML document (or else you can use your own XML document).
  • Add the following dependencies for the pom.xml file.

 org.apache.ws.commons.axiom
     axiom-api
     1.2.14


    org.apache.ws.commons.axiom
    axiom-impl
    1.2.14
  • Then we will create OMElement for the given XML document

Create a java file and build the OMElement using your path to the XML file.

OMElement documentElement= OMElementBuilder(filePath);

Creating the SOAP envelope

SOAPEnvelope envelope = buildSOAP12Envelope(documentElement);

Create AXIOM paths to access elements in the object

OMElement rootElement=documentElement;
 
System.out.println("nn ----------------------- AXIOM Path for selecting the brand of first mobile phone -----------------------");
AXIOMXPath xpath1 = new AXIOMXPath("//mobilephones/mobilephone[1]/model[1]");
OMElement selectedNode1 = (OMElement) xpath1.selectSingleNode(rootElement);

System.out.println(selectedNode1.getText());

System.out.println("nn-----------------------AXIOM Path for selecting names of mobile phones with weight less than 150g-----------------------");
AXIOMXPath xpath2 = new AXIOMXPath("//mobilephones/mobilephone[weight<150]/name");
List<OMElement> list=xpath2.selectNodes(rootElement);
for(OMElement o:list){
   System.out.println(o.getText());
}
System.out.println("nn-----------------------AXIOM Path for selecting brand of mobile phones which starts with 'Xperia'-----------------------");
AXIOMXPath xpath3 = new AXIOMXPath("//mobilephones/mobilephone[starts-with(name, 'Xperia')]/brand");
List<OMElement> list2=xpath3.selectNodes(rootElement);

for(OMElement o:list2){
   System.out.println(o.getText());
}

This will be the output generated by the above file.

AXIOM Path for selecting the brand of first mobile phone
D6563

AXIOM Path for selecting names of mobile phones with weight less than 150g
Lucid 3
Galaxy S Duos 2

AXIOM Path for selecting brand of mobile phones which starts with 'Xperia'

Sony

You can check with any XPath expressions. Check my post on XPath for more details abount XPAth.
See you again with another session soon.

Leave a comment

Your email address will not be published. Required fields are marked *