436ba7a3d784c7fe492e9cf015f7b8d35a5a777f
[tatoo.git] / tools / XPathEval.java
1 import javax.xml.xpath.*;
2 import org.xml.sax.*;
3 import org.w3c.dom.*;
4 import javax.xml.transform.*;
5 import javax.xml.transform.dom.*;
6 import javax.xml.transform.stream.*;
7
8 public class XPathEval {
9
10
11   public static void main(String args[]) {
12     try {
13
14
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());
28           break;
29         default:
30           serializer.transform(new DOMSource(nodes.item(i)), o);
31           break;
32         };
33         System.out.println();
34       };
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);
40     };
41
42
43   }
44 }