-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathXPathTest.java
executable file
·75 lines (61 loc) · 2.16 KB
/
XPathTest.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.io.File;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class XPathTest {
private static void debug(Object object){
System.out.print(" DEBUG ");
System.out.println(object);
}
private static void error(Object object){
System.out.print(" ERROR ");
System.out.println(object);
}
public static void main(String[] args){
new XPathTest();
}
public XPathTest(){
try{
Document document=getXmlByPath("c:\\temp.xml");
setParameterForGet(document);
}catch(Exception ex){
}
}
private void setParameterForGet(Document document){
try{
XPathFactory xpathFactory=XPathFactory.newInstance();
XPath xpath=xpathFactory.newXPath();
XPathExpression expression=xpath.compile("//response/results/*");
NodeList nodeSet=(NodeList)expression.evaluate(document, XPathConstants.NODESET);
debug("//response/results/ length:"+nodeSet.getLength());
if(nodeSet.getLength()>0){
/* this.parametersForGet.clear();
for(int counter=0;counter<nodeSet.getLength();counter++){
this.parametersForGet.add(new Parameter(nodeSet.item(counter)));
}
*/
}
}catch(Exception ex){
error("setParameterForGet: set parameter's Exception:"+ex.getMessage());
}
}
/** ïîëó÷èòü XML ôàéë èç óêàçàííîãî àáñîëþòíîãî ïóòè */
private Document getXmlByPath(String path_to_xml) throws Exception{
Document return_value=null;
javax.xml.parsers.DocumentBuilderFactory document_builder_factory=javax.xml.parsers.DocumentBuilderFactory.newInstance();
// óñòàíîâèòü íåïðîâåðÿåìîå ïîðîæäåíèå Parser-îâ
document_builder_factory.setValidating(false);
try {
// ïîëó÷åíèå àíàëèçàòîðà
javax.xml.parsers.DocumentBuilder parser=document_builder_factory.newDocumentBuilder();
return_value=parser.parse(new File(path_to_xml));
}catch(Exception ex){
error("XmlExchange.java ERROR");
throw ex;
}
return return_value;
}
}