package com.javahonk.xpath;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ReadXMLFileUsingXPath {

	public static void main(String[] args) {
		try {
			File xmlFile = new File("resources/RedboxMovieStore.xml");

			DocumentBuilderFactory builderFactory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder builder = builderFactory.newDocumentBuilder();

			Document xmlDocument = builder.parse(xmlFile);

			XPath xPath = XPathFactory.newInstance().newXPath();
			
			System.out.println("Select all movie title");
            String expression = "/Redbox/Movie/title";
            NodeList nodeList = (NodeList) xPath.compile(expression)
            		.evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                		.getFirstChild().getNodeValue()); 
            }
            
			
			System.out.println("Select movie price>15");
			expression = "/Redbox/Movie[price>15]/price";
			nodeList = (NodeList) xPath.compile(expression)
					.evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                		.getFirstChild().getNodeValue()); 
            }
            
            
            System.out.println("Select title where price>15");
            expression = "/Redbox/Movie[price>15]/title";
            nodeList = (NodeList) xPath.compile(expression)
            		.evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                		.getFirstChild().getNodeValue()); 
            }
            
            
            System.out.println("Select movie name where category is Kids");
            expression = "/Redbox/Movie[@category='Kids']/title";
            nodeList = (NodeList) xPath.compile(expression)
            		.evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                		.getFirstChild().getNodeValue()); 
            } 
            
            
            System.out.println("Select movie where category is kids");
            expression = "/Redbox/Movie[@category='Kids']";
            Node node = (Node) xPath.compile(expression)
            		.evaluate(xmlDocument, XPathConstants.NODE);
            if(null != node) {
                nodeList = node.getChildNodes();
                for (int i = 0;null!=nodeList && 
                		i < nodeList.getLength(); i++) {
                    Node nod = nodeList.item(i);
                    if(nod.getNodeType() == Node.ELEMENT_NODE)
                        System.out.println(nodeList.item(i)
                        		.getNodeName() + " : " 
                        		+ nod.getFirstChild().getNodeValue()); 
                }
            }
 
                        
            System.out.println("Select movie directors");
            expression = "/Redbox/Movie/Director";
            nodeList = (NodeList) xPath.compile(expression)
            		.evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                		.getFirstChild().getNodeValue()); 
            }
            
                        
            System.out.println("Select director name "
            		+ "where category = Action");
            expression = "/Redbox/Movie[@category='Action']/Director";
            nodeList = (NodeList) xPath.compile(expression)
            		.evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                		.getFirstChild().getNodeValue()); 
            }
            
            System.out.println("Select all category");            
            expression = "/Redbox/Movie/@category";
            nodeList = (NodeList) xPath.compile(expression)
            		.evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                		.getFirstChild().getNodeValue()); 
            }
            
            System.out.println("Select first category");            
            expression = "/Redbox/Movie[1]/@category";
            nodeList = (NodeList) xPath.compile(expression)
            		.evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                		.getFirstChild().getNodeValue()); 
            }
            
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			e.printStackTrace();
		}
	}

}
