2
* Licensed to the Apache Software Foundation (ASF) under one
3
* or more contributor license agreements. See the NOTICE file
4
* distributed with this work for additional information
5
* regarding copyright ownership. The ASF licenses this file
6
* to you under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
19
* $Id: ApplyXPath.java,v 1.2 2009/12/10 03:18:42 matthewoliver Exp $
21
// This file uses 4 space indents, no tabs.
23
import java.io.FileInputStream;
24
import java.io.OutputStreamWriter;
26
import javax.xml.parsers.DocumentBuilderFactory;
27
import javax.xml.transform.OutputKeys;
28
import javax.xml.transform.Transformer;
29
import javax.xml.transform.TransformerFactory;
30
import javax.xml.transform.dom.DOMSource;
31
import javax.xml.transform.stream.StreamResult;
33
import org.apache.xpath.XPathAPI;
34
import org.w3c.dom.Document;
35
import org.w3c.dom.Node;
36
import org.w3c.dom.traversal.NodeIterator;
37
import org.xml.sax.InputSource;
40
* Very basic utility for applying an XPath epxression to an xml file and printing information
41
/ about the execution of the XPath object and the nodes it finds.
44
* (2) an XPath expression to apply to the file
46
* java ApplyXPath foo.xml /
47
* java ApplyXPath foo.xml /doc/name[1]/@last
50
public class ApplyXPath
52
protected String filename = null;
53
protected String xpath = null;
55
/** Process input args and execute the XPath. */
56
public void doMain(String[] args)
62
if ((filename != null) && (filename.length() > 0)
63
&& (xpath != null) && (xpath.length() > 0))
65
// Tell that we're loading classes and parsing, so the time it
66
// takes to do this doesn't get confused with the time to do
67
// the actual query and serialization.
68
System.out.println("Loading classes, parsing "+filename+", and setting up serializer");
70
// Set up a DOM tree to query.
71
InputSource in = new InputSource(new FileInputStream(filename));
72
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
73
dfactory.setNamespaceAware(true);
74
Document doc = dfactory.newDocumentBuilder().parse(in);
76
// Set up an identity transformer to use as serializer.
77
Transformer serializer = TransformerFactory.newInstance().newTransformer();
78
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
80
// Use the simple XPath API to select a nodeIterator.
81
System.out.println("Querying DOM using "+xpath);
82
NodeIterator nl = XPathAPI.selectNodeIterator(doc, xpath);
84
// Serialize the found nodes to System.out.
85
System.out.println("<output>");
88
while ((n = nl.nextNode())!= null)
91
// DOM may have more than one node corresponding to a
92
// single XPath text node. Coalesce all contiguous text nodes
94
StringBuffer sb = new StringBuffer(n.getNodeValue());
96
Node nn = n.getNextSibling();
98
nn = nn.getNextSibling()
100
sb.append(nn.getNodeValue());
102
System.out.print(sb);
105
serializer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(System.out)));
107
System.out.println();
109
System.out.println("</output>");
113
System.out.println("Bad input args: " + filename + ", " + xpath);
117
/** Decide if the node is text, and so must be handled specially */
118
static boolean isTextNode(Node n) {
121
short nodeType = n.getNodeType();
122
return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
125
/** Main method to run from the command line. */
126
public static void main (String[] args)
129
if (args.length != 2)
131
System.out.println("java ApplyXPath filename.xml xpath\n"
132
+ "Reads filename.xml and applies the xpath; prints the nodelist found.");
136
ApplyXPath app = new ApplyXPath();
140
} // end of class ApplyXPath