2
* Copyright 2001-2004 The Apache Software Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
import java.io.OutputStreamWriter;
19
import javax.xml.namespace.*;
20
import javax.xml.transform.*;
21
import javax.xml.transform.dom.DOMSource;
22
import javax.xml.transform.stream.StreamResult;
23
import javax.xml.xpath.*;
28
* JAXP 1.3 XPath API sample.
30
* Basic utility for applying an XPath expression to an input xml file and printing
31
* the evaluation result, using JAXP 1.3 XPath API.
34
* (1) an xml file name
35
* (2) an XPath expression to apply to the input document
36
* (3) the return type, which is one of the following Strings:
37
* num, bool, str, node, nodeset
40
* java ApplyXPathJAXP foo.xml /doc/name[1]/@last str
41
* java ApplyXPathJAXP foo.xml /doc/name nodeset
43
public class ApplyXPathJAXP
45
public static void main(String[] args)
47
QName returnType = null;
51
System.err.println("Usage: java ApplyXPathAPI xml_file xpath_expression type");
54
InputSource xml = new InputSource(args[0]);
55
String expr = args[1];
57
// set the return type
58
if (args[2].equals("num")) returnType = XPathConstants.NUMBER;
59
else if (args[2].equals("bool")) returnType = XPathConstants.BOOLEAN;
60
else if (args[2].equals("str")) returnType = XPathConstants.STRING;
61
else if (args[2].equals("node")) returnType = XPathConstants.NODE;
62
else if (args[2].equals("nodeset")) returnType = XPathConstants.NODESET;
64
System.err.println("Invalid return type: " + args[2]);
67
XPathFactory factory = XPathFactory.newInstance();
68
XPath xpath = factory.newXPath();
72
// compile the XPath expression
73
XPathExpression xpathExpr = xpath.compile(expr);
75
// Evaluate the XPath expression against the input document
76
result = xpathExpr.evaluate(xml, returnType);
78
// Print the result to System.out.
87
* Print the type and value of the evaluation result.
89
static void printResult(Object result)
92
if (result instanceof Double) {
93
System.out.println("Result type: double");
94
System.out.println("Value: " + result);
96
else if (result instanceof Boolean) {
97
System.out.println("Result type: boolean");
98
System.out.println("Value: " + ((Boolean)result).booleanValue());
100
else if (result instanceof String) {
101
System.out.println("Result type: String");
102
System.out.println("Value: " + result);
104
else if (result instanceof Node) {
105
Node node = (Node)result;
106
System.out.println("Result type: Node");
107
System.out.println("<output>");
109
System.out.println("</output>");
111
else if (result instanceof NodeList) {
112
NodeList nodelist = (NodeList)result;
113
System.out.println("Result type: NodeList");
114
System.out.println("<output>");
115
printNodeList(nodelist);
116
System.out.println("</output>");
120
/** Decide if the node is text, and so must be handled specially */
121
static boolean isTextNode(Node n)
125
short nodeType = n.getNodeType();
126
return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
129
static void printNode(Node node)
132
if (isTextNode(node)) {
133
System.out.println(node.getNodeValue());
136
// Set up an identity transformer to use as serializer.
137
Transformer serializer = TransformerFactory.newInstance().newTransformer();
138
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
139
serializer.transform(new DOMSource(node), new StreamResult(new OutputStreamWriter(System.out)));
144
static void printNodeList(NodeList nodelist)
149
// Set up an identity transformer to use as serializer.
150
Transformer serializer = TransformerFactory.newInstance().newTransformer();
151
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
153
for (int i = 0; i < nodelist.getLength(); i++)
155
n = nodelist.item(i);
157
// DOM may have more than one node corresponding to a
158
// single XPath text node. Coalesce all contiguous text nodes
160
StringBuffer sb = new StringBuffer(n.getNodeValue());
162
Node nn = n.getNextSibling();
164
nn = nn.getNextSibling()
166
sb.append(nn.getNodeValue());
168
System.out.print(sb);
171
serializer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(System.out)));
173
System.out.println();
b'\\ No newline at end of file'
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
import java.io.OutputStreamWriter;
21
import javax.xml.namespace.*;
22
import javax.xml.transform.*;
23
import javax.xml.transform.dom.DOMSource;
24
import javax.xml.transform.stream.StreamResult;
25
import javax.xml.xpath.*;
30
* JAXP 1.3 XPath API sample.
32
* Basic utility for applying an XPath expression to an input xml file and printing
33
* the evaluation result, using JAXP 1.3 XPath API.
36
* (1) an xml file name
37
* (2) an XPath expression to apply to the input document
38
* (3) the return type, which is one of the following Strings:
39
* num, bool, str, node, nodeset
42
* java ApplyXPathJAXP foo.xml /doc/name[1]/@last str
43
* java ApplyXPathJAXP foo.xml /doc/name nodeset
45
public class ApplyXPathJAXP
47
public static void main(String[] args)
49
QName returnType = null;
53
System.err.println("Usage: java ApplyXPathAPI xml_file xpath_expression type");
56
InputSource xml = new InputSource(args[0]);
57
String expr = args[1];
59
// set the return type
60
if (args[2].equals("num")) returnType = XPathConstants.NUMBER;
61
else if (args[2].equals("bool")) returnType = XPathConstants.BOOLEAN;
62
else if (args[2].equals("str")) returnType = XPathConstants.STRING;
63
else if (args[2].equals("node")) returnType = XPathConstants.NODE;
64
else if (args[2].equals("nodeset")) returnType = XPathConstants.NODESET;
66
System.err.println("Invalid return type: " + args[2]);
69
XPathFactory factory = XPathFactory.newInstance();
70
XPath xpath = factory.newXPath();
74
// compile the XPath expression
75
XPathExpression xpathExpr = xpath.compile(expr);
77
// Evaluate the XPath expression against the input document
78
result = xpathExpr.evaluate(xml, returnType);
80
// Print the result to System.out.
89
* Print the type and value of the evaluation result.
91
static void printResult(Object result)
94
if (result instanceof Double) {
95
System.out.println("Result type: double");
96
System.out.println("Value: " + result);
98
else if (result instanceof Boolean) {
99
System.out.println("Result type: boolean");
100
System.out.println("Value: " + ((Boolean)result).booleanValue());
102
else if (result instanceof String) {
103
System.out.println("Result type: String");
104
System.out.println("Value: " + result);
106
else if (result instanceof Node) {
107
Node node = (Node)result;
108
System.out.println("Result type: Node");
109
System.out.println("<output>");
111
System.out.println("</output>");
113
else if (result instanceof NodeList) {
114
NodeList nodelist = (NodeList)result;
115
System.out.println("Result type: NodeList");
116
System.out.println("<output>");
117
printNodeList(nodelist);
118
System.out.println("</output>");
122
/** Decide if the node is text, and so must be handled specially */
123
static boolean isTextNode(Node n)
127
short nodeType = n.getNodeType();
128
return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
131
static void printNode(Node node)
134
if (isTextNode(node)) {
135
System.out.println(node.getNodeValue());
138
// Set up an identity transformer to use as serializer.
139
Transformer serializer = TransformerFactory.newInstance().newTransformer();
140
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
141
serializer.transform(new DOMSource(node), new StreamResult(new OutputStreamWriter(System.out)));
146
static void printNodeList(NodeList nodelist)
151
// Set up an identity transformer to use as serializer.
152
Transformer serializer = TransformerFactory.newInstance().newTransformer();
153
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
155
for (int i = 0; i < nodelist.getLength(); i++)
157
n = nodelist.item(i);
159
// DOM may have more than one node corresponding to a
160
// single XPath text node. Coalesce all contiguous text nodes
162
StringBuffer sb = new StringBuffer(n.getNodeValue());
164
Node nn = n.getNextSibling();
166
nn = nn.getNextSibling()
168
sb.append(nn.getNodeValue());
170
System.out.print(sb);
173
serializer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(System.out)));
175
System.out.println();