Monday, November 22, 2010

XPath another xml parser

In this period I am working with xml...lot of technologies try to solve the problem of retrieve information from an xml file like Dom, Sax and Jaxb. They all work very fine, however the simple and more easy tools that I found (and use ;) ) for me is xPath.
XPath is a query language designed for querying XML documents that uses path expressions to navigate XML documents. In order to define paths, we have to use a sintax which is similar to the one used for describing paths to files in operating systems.

Let us now start by describing a very simple example of how xPath work.
First of all let me show here a simple example of the xml file. This XML describes the employees that work inside a company.

<Personnel>
    <Employee type="contract">
       <Name>Robin</Name>
       <Salary>3011</Salary>
       <Id>3675</Id>
       <Age>25</Age>
    </Employee>
</Personnel>

Now, if we want to get the information about the age of the Employee named Robin we have to search inside the xml searching for the employee named Robin and, one found it, we can get the age by using the path
"/Personnel/Employee/Age"

A simple and useful code that help us to obtain every information about the employee is shown here:


public class SearchEmplyeesUsingXPath {
    private static String name ="Seagull";
   
     public static void main(String[] args) {

            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true);
            DocumentBuilder builder = null;
            try {
                builder = domFactory.newDocumentBuilder();
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Document doc = null;
            try {
                doc = builder.parse("employees.xml");
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = null;
            XPathExpression expr2 = null;
            XPathExpression expr3 = null;
            XPathExpression expr4 = null;
            try {
/**
*Here we get the text that we found at this path.  Note that text() retrieve the
* context associated to such path
**/
                expr = xpath.compile("/Personnel/Employee/Name/text()");
                expr2 = xpath.compile("/Personnel/Employee/Salary/text()");
                expr3 = xpath.compile("/Personnel/Employee/Id/text()");
                expr4 = xpath.compile("/Personnel/Employee/Age/text()");
               
            } catch (XPathExpressionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Object result;
            Object salary;
            Object id;
            Object age;
            try {
 /**
* Get the whole node in which we have found that path
**/
                result = expr.evaluate(doc, XPathConstants.NODESET);
                salary = expr2.evaluate(doc, XPathConstants.NODESET);
                id = expr3.evaluate(doc, XPathConstants.NODESET);
                age = expr4.evaluate(doc, XPathConstants.NODESET);
               
               
                NodeList nodes = (NodeList) result;
                NodeList nSal = (NodeList) salary;
                NodeList ids  = (NodeList) id;
                NodeList ages  = (NodeList) age;
                result = getInfo(nodes,nSal,ids,ages, name);
                System.out.println(result);
              
              
            } catch (XPathExpressionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          }

/**
* Print method
 **/
    private static Object getInfo(NodeList nodes, NodeList nSal, NodeList ids,
            NodeList ages, String name) {
        String result ="";
        for (int i = 0; i < nodes.getLength(); i++) {
            if(nodes.item(i).getNodeValue().equals(name)){   
   
                result+=nodes.item(i).getNodeValue()+"\n";
                result+=nSal.item(i).getNodeValue()+"\n";
                result+=ids.item(i).getNodeValue()+"\n";
                result+=ages.item(i).getNodeValue()+" \n\n\n";
            }
           
           
        }
        return result;
    }
}


For further information about XPath take a look here
http://www.w3.org/TR/xpath20/

No comments:

Post a Comment