1 import javax.xml.xpath.*;
4 import javax.xml.transform.*;
5 import javax.xml.transform.dom.*;
6 import javax.xml.transform.stream.*;
8 public class XPathEval {
11 public static void main(String args[]) {
15 XPath xpath = XPathFactory.newInstance().newXPath();
16 String expression = args[1];
17 InputSource inputSource = new InputSource(args[0]);
18 NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
19 Transformer serializer = TransformerFactory.newInstance().newTransformer();
20 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
21 StreamResult o = new StreamResult(System.out);
22 System.out.println("<xml_result>");
23 for(int i = 0; i < nodes.getLength(); i++){
24 serializer.transform(new DOMSource(nodes.item(i)), o);
25 System.out.println("");
27 System.out.println("</xml_result>");
28 } catch (XPathException e) {
29 System.out.println (e.getCause());
30 } catch (Exception e) {
31 System.out.println(e);