-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathXPathParser.java
36 lines (29 loc) · 1.06 KB
/
XPathParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/*
* http://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/package-summary.html
*/
public class XPathParser {
public static void main(String [] args) throws Exception {
if (args.length < 1) {
System.out.println("Usage:");
System.out.println("java XPathParser <XML File>");
System.exit(0);
}
String fileName = args[0];
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/html/body/cs378/assignments/assignment";
//String expression = "response/meetings/a [href]";
//String expression = "html/body/table/tr/a [href]";
InputSource inputSource = new InputSource(fileName);
NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
for(int i=0; i<nodes.getLength(); i++) {
Node n = nodes.item(i);
System.out.println("Node value:" + n.getTextContent());
}
}
}