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 Node n = nodes.item(i);
25 switch (n.getNodeType()) {
26 case Node.ATTRIBUTE_NODE:
27 System.out.print (n.getNodeName() + "=" + n.getNodeValue());
30 serializer.transform(new DOMSource(nodes.item(i)), o);
35 System.out.println("</xml_result>");
36 } catch (XPathException e) {
37 System.out.println (e.getCause());
38 } catch (Exception e) {
39 System.out.println(e);