Showing posts with label XPath. Show all posts
Showing posts with label XPath. Show all posts

Sunday, July 20, 2014

AXIOM

Introduction to AXIOM

Hi,


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.


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
<?xml version="1.0" encoding="UTF-8"?>
<mobilephones>
    <mobilephone>
        <model>D6563</model>
        <name>Xperia Z2a</name>
        <brand>Sony</brand>
        <released-year>2014</released-year>
        <dimensions>
            <height>137mm</height>
            <width>72mm</width>
            <depth>11mm</depth>
        </dimensions>
        <weight>163</weight>
    </mobilephone>

    <mobilephone>
        <model>VS876</model>
        <name>Lucid 3</name>
        <brand>LG</brand>
        <released-year>2013</released-year>
        <dimensions>
            <height>131.6mm</height>
            <width>66mm</width>
            <depth>9.9mm</depth>
        </dimensions>
        <weight>123.9</weight>
    </mobilephone>
</mobilephones>

  • 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.

<dependency>
 <groupId>org.apache.ws.commons.axiom</groupId>
     <artifactId>axiom-api</artifactId>
     <version>1.2.14</version>
</dependency>
<dependency>
    <groupId>org.apache.ws.commons.axiom</groupId>
    <artifactId>axiom-impl</artifactId>
    <version>1.2.14</version>
</dependency>

  • 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("\n\n ----------------------- 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("\n\n-----------------------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("\n\n-----------------------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.

Friday, July 18, 2014

Learning XPath

Hi all...

Today we are going to learn about XPath.

XPath introduction

First of all we will try to understand, what is XPath. 

XML has a hierarchical structure with parent and child nodes. So we should have a mechanism to navigate through these tags to get their values. XPath is used to do this task. XPath is a major component of XSLT and you must have a good knowledge about it to write XSLT documents.

Let's see how XPath is going to work.

XPath path expressions

The top most element in an XML document is called the root node. The root node is expressed in XPath as '/'.

Let's go to deep with an example. I am going to choose my old favorite example XML document.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/home/dinithi/workspace/PhoneShop/src/StyleSheetXML.xsl"?>
<mobilephones>
 <mobilephone id="1">
  <model>D6563</model>
  <name>Xperia Z2a</name>
  <brand>Sony</brand>
  <released-year>2014</released-year>
  <dimensions>
   <height>137mm</height>
   <width>72mm</width>
   <depth>11mm</depth>
  </dimensions>
  <weight>163g</weight>
  <feature>
   <os>Android v4.4.2</os>
   <CPU>Quad-core 2.3 GHz</CPU>
   <browser>HTML 5</browser>
  </feature>
 </mobilephone>

 <mobilephone id="2">
  <model>VS876</model>
  <name>Lucid 3</name>
  <brand>LG</brand>
  <released-year>2012</released-year>
  <dimensions>
   <height>131.6mm</height>
   <width>66mm</width>
   <depth>9.9mm</depth>
  </dimensions>
  <weight>123.9g</weight>
  <feature>
   <os>Android v4.4.2</os>
   <CPU>Quad-core 1.2 GHz</CPU>
   <browser>HTML</browser>
  </feature>
 </mobilephone>


 <mobilephone id="3">
  <model>S7582</model>
  <name>Galaxy S Duos 2</name>
  <brand>Samsung</brand>
  <released-year>2013</released-year>
  <dimensions>
   <height>121.5mm</height>
   <width>63.1mm</width>
   <depth>10.6mm</depth>
  </dimensions>
  <weight>118g</weight>
  <feature>
   <os>Android v4.2</os>
   <CPU>Dual-core 1.2 GHz</CPU>
   <browser>HTML</browser>
  </feature>
 </mobilephone>
</mobilephones>


You can use /mobilephones or /mobilephones/mobilephone/model to select from the root node.
Or you can select as model and it will search for all the nodes with model tags. This way is not good for large XML documents.
If you select as //model if you really do not know the root node or you know only a part of your XML.

To select attributes, we can use @ symbol. For example if you need to get the id attribute you can use @id or for all attributes @* .

A dot '.' can represent current node and '..' can represent the parent node.

I know it is a mess for the beginners. You can understand clearly with the following examples. Try them in your own.

Ex 1:

Get the model of the first mobile phone. So we can write the path as follows.

//mobilephone[1]/model/text()

This will give the result as Text='D6563' .

Let's examine this syntax.
First it goes to the mobilephone tag and get the list of mobilephones. We have used [1] here. So it will check for the first mobilephone tag. Then /model is going inside the model tag and text() is getting the output value.

Ex 2:

Get the brand of mobiles phones which were released before 2014.

//mobilephone[released-year<2014]/brand/text()

This will give the result as Text='Samsung'.

Ex 3:

Get the brands of all phones which have 'Xperia' as a part of its name.

//mobilephones/mobilephone[starts-with(name, 'Xperia')]/brand

This will give the result as Element='<brand>Sony</brand>' .

Ex 4:

Get the count of available mobile phone nodes.


count(//mobilephones/mobilephone/brand)

This will give the output as Double='3.0' .

Ex 5:

Get the name of the mobile phone which has the id as 1.


//mobilephone[@id='1']/name/text()

This will give the output as Text='Xperia Z2a' .


Hope you can understand what is done by XPath. There are more functions available to work with XPath. Try to get sum, sort like expressions...

See you again...