/*
* Created on 2005/01/24
*
* Introduce XPath
*/
package org.brunt.xml.xpath;
import java.io.StringBufferInputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.InputSource;
/**
* @author ht
*
* Test XPath
*/
public class TestXPath {
public static final String description =
" http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html";
public static final String XPATH[] = {
//example1: start with '/' express a absoulte path
"/AAA", "/AAA/XXX/DDD", "/AAA/XXX/DDDNO",
//example2: start with '//' express all element
"//BBB", "//BBB/BBB", "//BBB/BBBNO",
//example3: '*' express all
"/AAA/XXX/DDD/*", "//DDD/*",
//example4: '[]' descript position
"/AAA/XXX/DDD/BBB[1]",
"/AAA/XXX/DDD/BBB[2]",
"/AAA/XXX/DDD/BBB[last()]", //last()
//example5: '@' descript attribute
"//@name", //"//@id",
"/AAA/@id",
"//DDD/@name",
"//DDD/@color",
"//BBB[ @*]",
"//BBB[not( @*)]",//not()
//example6:
"//BBB[ @color='yellow']",
"//BBB[ @color='red']",
"//BBB[normalize-space( @color)='red']",//normalize-space()
//example7: count()
"//*[count(BBB)=3]", //count()
"//*[count(*)=1]", //*
//example8: name() starts-with() contains()
"//*[name()='BBB']", //name()
"//*[starts-with(name(),'D')]", //starts-with()
"//*[contains(name(),'E')]", //contains()
//example9:
"//*[string-length(name()) = 5]", //string-length()
"//*[string-length(name()) < 5]",
//example10: connect with some path by '|'
"//BBB[ @id='b1'] | //EEE | //FFF",
//example11:
"/child::AAA", //child::
"/AAA", // /child::AAA == AAA
//example12:
"/AAA/XXX/DDD/descendant::*", //descendant::
"//DDD/descendant::*",
//example13:
"//BBB/parent::*", //parent::
//example14:
"/AAA/XXX/DDD/BBB/ancestor::*", //ancestor::
"/AAA/XXX/DDD/BBB/ancestor::XXX",
//example15:
"/AAA/XXX/following-sibling::*", //following-sibling::
//example16:
"/AAA/CCC/preceding-sibling::*", //preceding-sibling::
//example17:
"/AAA/XXX/following::*", //following::
//example18:
"/AAA/XXX/preceding::*", //preceding::
//example19:
"/AAA/XXX/descendant-or-self::*", //descendant-or-self::
//example20:
"/AAA/XXX/DDD/EEE/ancestor-or-self::*", ///ancestor-or-self::
//example21:
"//BBB/self::*",
"//EEE/ancestor::* | //EEE/descendant::* | //EEE/following::* | //EEE/preceding::* | //EEE/self::*",
//example22:
"//BBB[position() mod 2 = 0 ]", //position() mod
"//BBB[ position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5) ]",
};
public static final String DEFAULT_XML_STRING =
" <AAA id=/"a1/"> /n"
+ " <XXX id=/"x1/"> /n"
+ " <DDD id=/"d1/" name=/"d1name/"> /n"
+ " <BBB id=/"b1/"/> /n"
+ " <BBB id=/"b2/"/> /n"
+ " <BBB/> /n"
+ " <EEE id=/"e1/"/> /n"
+ " <FFF id=/"f1/"/> /n"
+ " </DDD> /n"
+ " <DEE id=/"dee1/">hello /n"
+ " </DEE> /n"
+ " <TEST1 id=/"test1/"/> /n"
+ " <TEST2 id=/"test2/"/> /n"
+ " </XXX> /n"
+ " <CCC id=/"c1/"> /n"
+ " <DDD id=/"d2/" name=/"d2name/" color=/"red/"> /n"
+ " <BBB id=/"b3/"/> /n"
+ " <BBB id=/"b4/"/> /n"
+ " <BBB id=/"b10/" color=/"yellow/"/> /n"
+ " <BBB id=/"b11/" color=/"red/"/> /n"
+ " <BBB id=/"b12/" color=/" red /"/> /n"
+ " <EEE id=/"e2/" name=/"e2name/"/> /n"
+ " <FFF id=/"f2/"/> /n"
+ " </DDD> /n"
+ " </CCC> /n"
+ " <CCC> /n"
+ " <BBB id=/"b5/"> /n"
+ " <BBB id=/"b6/"> /n"
+ " <BBB id=/"b7/"/> /n"
+ " </BBB> /n"
+ " </BBB> /n"
+ " </CCC> /n"
+ " </AAA> /n ";
public static void printDefaultXml() {
System.out.println(DEFAULT_XML_STRING);
}
public static InputSource getDefaultInputSource() {
return new InputSource(new StringBufferInputStream(DEFAULT_XML_STRING));
}
public static Document getDocument(InputSource is) throws Exception {
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
return dfactory.newDocumentBuilder().parse(is);
}
private static void printNodeIterator(NodeIterator ni) {
Node node = null;
while ((node = ni.nextNode()) != null) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.print("/t[NodeName]:" + node.getNodeName());
System.out.print("/t[NodeValue]:" + node.getNodeValue());
printNodeAttrId(node);
} else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
System.out.println(
"/t/t[ATTR]:"
+ node.getNodeName()
+ "="
+ node.getNodeValue());
}
}
}
private static void printNodeAttrId(Node node) {
String id = "null";
if (node.hasAttributes()) {
Node attrNode = node.getAttributes().getNamedItem("id");
if (attrNode != null)
id = attrNode.getNodeValue();
}
System.out.println("/t/t[NodeId]:" + id);
}
public void run() throws Exception {
run(null);
}
public void run(InputSource is) throws Exception {
Document doc = getDocument(is == null ? getDefaultInputSource() : is);
for (int i = 0; i < XPATH.length; i++)
run(XPATH[i], doc);
}
public void run(String xpath, Document doc) throws Exception {
System.out.println("[XPATH]:" + xpath);
NodeIterator nl = XPathAPI.selectNodeIterator(doc, xpath);
printNodeIterator(nl);
System.out.println("/n");
}
public static void main(String[] args) throws Exception {
printDefaultXml();
TestXPath txpath = new TestXPath();
txpath.run();
}
}
* Created on 2005/01/24
*
* Introduce XPath
*/
package org.brunt.xml.xpath;
import java.io.StringBufferInputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.InputSource;
/**
* @author ht
*
* Test XPath
*/
public class TestXPath {
public static final String description =
" http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html";
public static final String XPATH[] = {
//example1: start with '/' express a absoulte path
"/AAA", "/AAA/XXX/DDD", "/AAA/XXX/DDDNO",
//example2: start with '//' express all element
"//BBB", "//BBB/BBB", "//BBB/BBBNO",
//example3: '*' express all
"/AAA/XXX/DDD/*", "//DDD/*",
//example4: '[]' descript position
"/AAA/XXX/DDD/BBB[1]",
"/AAA/XXX/DDD/BBB[2]",
"/AAA/XXX/DDD/BBB[last()]", //last()
//example5: '@' descript attribute
"//@name", //"//@id",
"/AAA/@id",
"//DDD/@name",
"//DDD/@color",
"//BBB[ @*]",
"//BBB[not( @*)]",//not()
//example6:
"//BBB[ @color='yellow']",
"//BBB[ @color='red']",
"//BBB[normalize-space( @color)='red']",//normalize-space()
//example7: count()
"//*[count(BBB)=3]", //count()
"//*[count(*)=1]", //*
//example8: name() starts-with() contains()
"//*[name()='BBB']", //name()
"//*[starts-with(name(),'D')]", //starts-with()
"//*[contains(name(),'E')]", //contains()
//example9:
"//*[string-length(name()) = 5]", //string-length()
"//*[string-length(name()) < 5]",
//example10: connect with some path by '|'
"//BBB[ @id='b1'] | //EEE | //FFF",
//example11:
"/child::AAA", //child::
"/AAA", // /child::AAA == AAA
//example12:
"/AAA/XXX/DDD/descendant::*", //descendant::
"//DDD/descendant::*",
//example13:
"//BBB/parent::*", //parent::
//example14:
"/AAA/XXX/DDD/BBB/ancestor::*", //ancestor::
"/AAA/XXX/DDD/BBB/ancestor::XXX",
//example15:
"/AAA/XXX/following-sibling::*", //following-sibling::
//example16:
"/AAA/CCC/preceding-sibling::*", //preceding-sibling::
//example17:
"/AAA/XXX/following::*", //following::
//example18:
"/AAA/XXX/preceding::*", //preceding::
//example19:
"/AAA/XXX/descendant-or-self::*", //descendant-or-self::
//example20:
"/AAA/XXX/DDD/EEE/ancestor-or-self::*", ///ancestor-or-self::
//example21:
"//BBB/self::*",
"//EEE/ancestor::* | //EEE/descendant::* | //EEE/following::* | //EEE/preceding::* | //EEE/self::*",
//example22:
"//BBB[position() mod 2 = 0 ]", //position() mod
"//BBB[ position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5) ]",
};
public static final String DEFAULT_XML_STRING =
" <AAA id=/"a1/"> /n"
+ " <XXX id=/"x1/"> /n"
+ " <DDD id=/"d1/" name=/"d1name/"> /n"
+ " <BBB id=/"b1/"/> /n"
+ " <BBB id=/"b2/"/> /n"
+ " <BBB/> /n"
+ " <EEE id=/"e1/"/> /n"
+ " <FFF id=/"f1/"/> /n"
+ " </DDD> /n"
+ " <DEE id=/"dee1/">hello /n"
+ " </DEE> /n"
+ " <TEST1 id=/"test1/"/> /n"
+ " <TEST2 id=/"test2/"/> /n"
+ " </XXX> /n"
+ " <CCC id=/"c1/"> /n"
+ " <DDD id=/"d2/" name=/"d2name/" color=/"red/"> /n"
+ " <BBB id=/"b3/"/> /n"
+ " <BBB id=/"b4/"/> /n"
+ " <BBB id=/"b10/" color=/"yellow/"/> /n"
+ " <BBB id=/"b11/" color=/"red/"/> /n"
+ " <BBB id=/"b12/" color=/" red /"/> /n"
+ " <EEE id=/"e2/" name=/"e2name/"/> /n"
+ " <FFF id=/"f2/"/> /n"
+ " </DDD> /n"
+ " </CCC> /n"
+ " <CCC> /n"
+ " <BBB id=/"b5/"> /n"
+ " <BBB id=/"b6/"> /n"
+ " <BBB id=/"b7/"/> /n"
+ " </BBB> /n"
+ " </BBB> /n"
+ " </CCC> /n"
+ " </AAA> /n ";
public static void printDefaultXml() {
System.out.println(DEFAULT_XML_STRING);
}
public static InputSource getDefaultInputSource() {
return new InputSource(new StringBufferInputStream(DEFAULT_XML_STRING));
}
public static Document getDocument(InputSource is) throws Exception {
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
return dfactory.newDocumentBuilder().parse(is);
}
private static void printNodeIterator(NodeIterator ni) {
Node node = null;
while ((node = ni.nextNode()) != null) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.print("/t[NodeName]:" + node.getNodeName());
System.out.print("/t[NodeValue]:" + node.getNodeValue());
printNodeAttrId(node);
} else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
System.out.println(
"/t/t[ATTR]:"
+ node.getNodeName()
+ "="
+ node.getNodeValue());
}
}
}
private static void printNodeAttrId(Node node) {
String id = "null";
if (node.hasAttributes()) {
Node attrNode = node.getAttributes().getNamedItem("id");
if (attrNode != null)
id = attrNode.getNodeValue();
}
System.out.println("/t/t[NodeId]:" + id);
}
public void run() throws Exception {
run(null);
}
public void run(InputSource is) throws Exception {
Document doc = getDocument(is == null ? getDefaultInputSource() : is);
for (int i = 0; i < XPATH.length; i++)
run(XPATH[i], doc);
}
public void run(String xpath, Document doc) throws Exception {
System.out.println("[XPATH]:" + xpath);
NodeIterator nl = XPathAPI.selectNodeIterator(doc, xpath);
printNodeIterator(nl);
System.out.println("/n");
}
public static void main(String[] args) throws Exception {
printDefaultXml();
TestXPath txpath = new TestXPath();
txpath.run();
}
}