import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import java.io.*; /** * User: herry * Date: 15-5-20 20:44 */ public class TestXPath { public static void main(String[] args) throws Exception { String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<rss version=\"2.0\">\n" + " <channel>\n" + " <title>Java Tutorials and Examples 2</title>\n" + " <language>en-us</language>\n" + " <item>\n" + " <title><![CDATA[Java Tutorials 2]]></title>\n" + " <link>http://www.javacodegeeks.com/</link>\n" + " </item>\n" + " <item>\n" + " <title><![CDATA[Java Examples 2]]></title>\n" + " <link>http://examples.javacodegeeks.com/</link>\n" + " </item>\n" + " </channel>\n" + "</rss>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new ByteArrayInputStream(testXML.getBytes("UTF-8"))); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); String expression; Node node; NodeList nodeList; // 1. root element expression = "/*"; node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE); System.out.println("1. " + node.getNodeName()); // 2. root element (by name) expression = "/rss"; node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE); System.out.println("2. " + node.getNodeName()); // 3. element under rss expression = "/rss/channel"; node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE); System.out.println("3. " + node.getNodeName()); // 4. all elements under rss/channel expression = "/rss/channel/*"; nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); System.out.print("4. "); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print(nodeList.item(i).getNodeName() + " "); } System.out.println(); // 4.1 all elements under rss/channel/title expression = "/rss/channel/item"; nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); System.out.print("4.1 "); for (int i = 0; i < nodeList.getLength(); i++) { Node node1 = nodeList.item(i); System.out.print(node1.getNodeName() + " "); /** * //para selects all the para descendants ofthe document root and thus selects all para elements in thesame document as the context node * .//para selects the para elementdescendants of the context node */ System.out.println("===="+((Element)xpath.evaluate("./title", node1, XPathConstants.NODE)).getTextContent()); } System.out.println(); // 5. all title elements in the document expression = "//title"; nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); System.out.print("5. "); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print(nodeList.item(i).getNodeName() + " "); } System.out.println(); // 6. all elements in the document except title expression = "//*[name() != 'title']"; nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); System.out.print("6. "); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print(nodeList.item(i).getNodeName() + " "); } System.out.println(); // 7. all elements with at least one child element expression = "//*[*]"; nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); System.out.print("7. "); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print(nodeList.item(i).getNodeName() + " "); } System.out.println(); // 8. all level-5 elements (the root being at level 1) expression = "/*/*/*/*"; nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); System.out.print("8. "); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print(nodeList.item(i).getNodeName() + " "); } System.out.println(); } }
相关推荐
Xpath测试工具 Xpath测试工具 Xpath测试工具Xpath测试工具Xpath测试工具Xpath测试工具
自己开发的工具,经过一系列完善,目前能直接测试XPath并获取结果,暂没共享源码,后期会上传源码,敬请关注!
xpath表达式测试小工具,很实用。推荐。上次不知道为什么被删掉了。重发。
基于framework 3.5的对正则表达式和xpath测试的小工具
xpath表达式测试用工具,可以试一下,还可以!!!!!!
Xpath测试工具,下载解压直接使用,复制内容到工具内,编写规则测试即可
升级版本.针对增加了部分标签的屏蔽功能做了处理.. 更好的对xpath进行分析和测试
58同城搜索页静态html代码爬虫xpath测试demo
仅需要输入目标网址+xpath规则即可进行测试xpath是否有效。 SHA256: bbaf93f27776e6b9c1cb614ea147bc29f91d7cb6d8bd1432f94bf4a246c24162 查毒地址: ...
测试XPath注入
主要用于自动化测试中IE浏览器下获取元素的XPath,经过测试,结果和chrome生成的一样,使用java,或python+selenium定位元素
对使用HtmlAgilityPack进行的测试...
根据报告中每条失效脚本的XPath和value信息,通过遍历新旧应用网页差异文档对象化模型解析树(DOM tree),找到替换失效脚本的路径或value值,从而修复失效的测试脚本。实验结果表明,该方法对修复失效测试脚本是...
C# 写的测试XPath 的小工具,用来测试写的Xpath 是否正确
Xpath生成器,自动生成可用的Xpath
xpath实用技巧总结 多个相同的标签如何定位,包含空格如何定位。
Selenium xpath,
xpath-helper
网上找的很好的XPATH指南,里面包含了22个实用例子,看完后,XPATH就懂了。
经典xpath教材打包, XPath是W3C定义的语言和正式的W3C推荐的语言,W3C拥有XML Path Language (XPath) Version 1.0规范。XPath诞生于1999年,作为对XSLT和XPointer语言的补充,但近来已成为流行的独立语言,因为单个...