public static void main(String[] args) {
 
        try {
        	
			 if(args.length == 2){
				 
 				String xpath=args[0];
 				String path =args[1];
 				
 		        FileInputStream file = new FileInputStream(new File(path));
                
 	            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
 	             
 	            DocumentBuilder builder =  builderFactory.newDocumentBuilder();
 	             
 	            Document xmlDocument = builder.parse(file);
 	 
 	            XPath xPath =  XPathFactory.newInstance().newXPath();
 	 
 	            String expression = xpath;
 	            String xml = xPath.compile(expression).evaluate(xmlDocument);
 	            
 				 String outfile="";
 				 if(path.toLowerCase().contains(".xml")){
 					 outfile= path.substring(0,path.indexOf(".xml"))+".out.xml";
 				 }
 				 else {
 					 outfile= path+".out.xml";
 				 }
 				 System.out.println(xml);
// 				Document document = builder.parse(new InputSource(new StringReader(xml)));
// 				writeNode(document,outfile);
 				try {
 					 
 		 
 					File out = new File(outfile);
 		 
 					// if file doesnt exists, then create it
 					if (!out.exists()) {
 						out.createNewFile();
 					}
 		 
 					FileWriter fw = new FileWriter(out.getAbsoluteFile());
 					BufferedWriter bw = new BufferedWriter(fw);
 					bw.write(xml);
 					bw.close();
 		 
 		 
 				} catch (IOException e) {
 					e.printStackTrace();
 				}
 				
 			 }
			 
    
 


 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }       
    }