Java-XPath(code)

该博客主要展示了XPath的测试代码。引入了相关包,定义了XPath表达式数组和默认XML字符串,包含多种XPath示例,如路径表达、属性描述等。还提供了打印XML、获取输入源、文档等方法,最后在main方法中运行测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
* 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();
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值