`
zero_dian
  • 浏览: 17835 次
  • 性别: Icon_minigender_1
  • 来自: 南京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

xpath使用效率探索

阅读更多
XPath使用效率
1.“/”与“//”的效率比较
String xpath1="/broadcast/contentList/item[@sendId=\"99\" and @receiveId=\"100\" and @resId=\"200\"]";
String xpath2="//item[@sendId=\"99\" and @receiveId=\"100\" and @resId=\"200\"]";
读同一个xml文件中,xpath1效率是xpath2效率的2倍到3倍

2.效率与节点层数的关系
“/”与节点层数影响不大,“//”与节点层数影响很大,我理解“/”可以迅速定位,而“//”需要查找整个xml文件

3.效率与记录集的关系
String xpath1="/broadcast/item[@sendId=\"99\" and @receiveId=\"100\" and @resId=\"200\"]"; //记录单一
String xpath2="/broadcast/contentList/item[@sendId=\"20742\" and @receiveId=\"4413033\" and @resId=\"0\"]";//记录多条
String xpath3="/broadcast/contentList/item";
在程序找到记录后,不做任何事情情况下,即光从xpath查找的效率考虑,此三个表达式效率相差不大,我理解是此三个表达式都需要查找所有的item节点。
这结果也同样适合与“//”

4.“//”使用效率
String xpath1="//item[@sendId=\"99\" and @receiveId=\"100\" and @resId=\"200\"]";
String xpath2="//contentList/item[@sendId=\"99\" and @receiveId=\"100\" and @resId=\"200\"]";
"//"定位越准确,效率会稍微提高点

5.xpath二次查找效率问题
即把一个xpath路径,分为二次查找,第一次查出xml中的部分节点,然后在查找出的节点下,查处所有的记录集
String xpath1="/broadcast/contentList/item[@sendId=\"20742\" and @receiveId=\"4413033\" and @resId=\"0\"]";
//String xpath1="/broadcast/contentList/item";

String path1="/broadcast";
//String path2="./item[@sendId=\"99\" and @receiveId=\"100\" and @resId=\"200\"]";
String path2="./contentList/item";
xpathTwice(fileName,path1,path2);

与"/"直接路径查找相比,影响不大,效率基本上相同。

测试程序:
package com.corry.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.XPath;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.xpath.DefaultXPath;

/**
* xpath dom4j效率试验
* @author corry sun
*/
public class TestXpath {

/**
* @param args
*/
public static void main(String[] args) {
//String xpath1="/broadcast/contentList/item[@sendId=\"99\" and @receiveId=\"100\" and @resId=\"200\"]";
//String xpath1="/broadcast/item[@sendId=\"99\" and @receiveId=\"100\" and @resId=\"200\"]";
//String xpath1="//item[@sendId=\"99\" and @receiveId=\"100\" and @resId=\"200\"]";
String xpath1="/broadcast/contentList/item";

//String xpath1="/broadcast/contentList/item[@sendId=\"20742\" and @receiveId=\"4413033\"]";
//String xpath1="//item[@sendId=\"20742\" and @receiveId=\"4413033\" and @resId=\"0\"]";
testTime("E:"+ File.separator+ "opt/broadcast.xml",xpath1);

//String xpath2="//item[@sendId=\"99\" and @receiveId=\"100\" and @resId=\"200\"]";
//testTime("E:"+ File.separator+ "opt/broadcast.xml",xpath2);


/**
* dom4j visitor遍历节点\
*/
/*Date begin=new Date();
Document doc=readXML("E:"+ File.separator+ "opt/broadcast.xml");
Element root=doc.getRootElement();
root.accept(new MyVisitor());
Date end=new Date();
long time=end.getTime()-begin.getTime();
System.out.println("执行使用时间为:"+time+"ms");*/

/**
* 删除节点,需要大量的时间
*/

/*String deleteXPath="//broadcast/contentList/item[position()<6]";
Document doc=readXML("E:"+ File.separator+ "opt/broadcast.xml");
if(deleteNodes(doc,deleteXPath)){
System.out.println("delete nodes success");

}else{
System.out.println("delete nodes false");
}*/

/**
* xpath二次查找效率和xpath直接查找差不多
*/
/*String fileName="E:"+ File.separator+ "opt/broadcast.xml";
String path1="/broadcast";
//String path2="./item[@sendId=\"99\" and @receiveId=\"100\" and @resId=\"200\"]";
String path2="./contentList/item";
xpathTwice(fileName,path1,path2);*/
}

/**
* 测试xpath程序运行时间
* @param fileName
* @param xpath
*/
public static void testTime(String fileName,String xpath){
Date begin=new Date();

Document doc=readXML(fileName);

Iterator iter=getChildren(doc,xpath).iterator();
while(iter.hasNext()){
Element ele=(Element)iter.next();
System.out.println(ele.asXML());
}

Date end=new Date();
long time=end.getTime()-begin.getTime();
System.out.println(xpath+",执行使用时间为:"+time+"ms");
}

/**
* 把xpath路径为两次查找
*
*/
public static void xpathTwice(String fileName,String path1,String path2){
Date begin=new Date();

Document doc=readXML(fileName);
Node node=getChild(doc,path1);
System.out.println(node.getName());
Iterator iter=getChildren(node,path2).iterator();
while(iter.hasNext()){
Element ele=(Element)iter.next();
System.out.println(ele.asXML());
}

Date end=new Date();
long time=end.getTime()-begin.getTime();
System.out.println(path1+path2+",执行使用时间为:"+time+"ms");
}

/**
* 使用dom4j的DeafaultXPath查出节点的List
* @param fileName
* @return
*/
public static Document readXML(String fileName){
Document doc = file2Document(fileName);
return doc;
}

public static List getChildren(Object ele, String childPath) {
if (ele == null || childPath == null)
return null;
XPath path = new DefaultXPath(childPath);
return path.selectNodes(ele);
}

/**
* 使用dom4j的DeafaultXPath查出某个单一节点
* @param element
* @param childPath
* @return
*/
public static Node getChild(Object element, String childPath) {
if (element == null || childPath == null)
return null;
XPath path = new DefaultXPath(childPath);
return path.selectSingleNode(element);
}

/**
*
* @param doc
* @param childPath
* @return
*/
public static boolean deleteNodes(Document doc,String childPath){
if (doc == null || childPath == null)
return false;
Iterator iter=getChildren(doc,childPath).iterator();
while(iter.hasNext()){
Element ele=(Element)iter.next();
Element parent=ele.getParent();
parent.remove(ele);
}
write(doc);
return true;
}

/**
* 写入xml文件
* @param doc
*/
public static void write(Document doc){
XMLWriter writer=null;
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
try {
writer=new XMLWriter(new FileWriter("E:"+ File.separator+ "opt/broadcast.xml"),format);
writer.write(doc);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 删除节点
* @param filePath
* @return
*/
public static Document file2Document(String filePath) {
Document doc = null;
FileInputStream fis = null;
try {
SAXReader saxReader = new SAXReader();
saxReader.setEncoding("UTF-8");
File f = new File(filePath);
if (f.exists()) {
fis = new FileInputStream(f);
doc = saxReader.read(fis);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return doc;
}
}

package com.corry.test;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;

public class MyVisitor extends VisitorSupport {
public void visit(Element element) {

System.out.println("element:"+element.getName());

}

public void visit(Attribute attr) {

System.out.println("attribute:"+attr.getName());

}

}
分享到:
评论

相关推荐

    xpath使用手册

    归纳整理的xpath使用手册,在自动化工作中,xpath使用很频繁。

    XPath 使用方法

    xpath的使用方法

    xpath使用文档,初学者非常有用

    xpath定位页面结点,轻松使用xpath来查找页面结点,使测试更简单,特别是集成selenium测试工具的使用,非常好用。

    Xpath生成器,自动生成XPATH,C#版

    Xpath生成器,自动生成可用的Xpath

    XPath注入漏洞利用工具XPath-XCat.zip

    XCat是一个命令行程序,用于辅助XPath注入漏洞的利用。XCat使用Python编写并开放源代码。XCat正常使用需要python的SimpleXMLWriter模块。 标签:XPath

    使用 XML XPath 2.0 入门

    虽然还是候选推荐标准,但 XPath 2.0 即将得到正式批准。这是 1999 年以来对 XPath 推荐标准的第一次修订,市场对此抱有很大期望,事实上一些工具已经开始实现最新的草案。这些修改是根本性的,我预料到时候人们也许...

    XPath教程

    介绍XSL语法中Xpath的使用方法

    xpath实例语法教程及IBM使用的xpath路径观察器

    同时还提供了IBM等大公司都在使用的xpath操作选择路径的观察器及验证器,该观察器及验证器功能很强大,你只要输入xpath查找路径,验证器会校验语法是否正确,同时会以鲜艳标志显示出xpath所需查找的路径节点。...

    xpath定位,xpath定位,xpath定位

    Selenium xpath,

    经典xpath教材打包

    使用XPath的目的:为了在匹配XML文档结构时能够准确地找到某一个节点元素。可以把XPath比作文件管理路径,通过文件管理路径,可以按照一定的规则查找到所需要的文件;同样,依据XPath所制定的规则,也可以很方便地...

    JsoupXpath

    整理JsoupXpath( https://github.com/zhegexiaohuozi/JsoupXpath)是一款纯Java开发的使用xpath解析提取html内容的解析器,xpath语法分析与执行完全独立,html的DOM树生成借助Jsoup,故命名为JsoupXpath. 为了在java...

    xpath-helper 插件及使用方法

    XPath Helper可以支持在网页点击元素生成xpath,整个抓取使用了xpath、正则表达式、消息中间件、多线程调度框架的chrome插件。

    Selenium中使用XPath.docx

    由以上表格可见,在IE下使用了Cybozu Lab的XPath library后,执行效率有了很大提升,基本上可以与使用Dom定位器相当。通过比较,在新的项目中使用Selenium来进行Web自动化开发,使用XPath定位器,可以使得定位器本生...

    Java中使用xpath获取xml中的数据

    使用xpath读取xml中的数据

    Xpath指南XPATH实例

    网上找的很好的XPATH指南,里面包含了22个实用例子,看完后,XPATH就懂了。

    xpath-helper.zip

    xpath-helper

    火狐老版本+xpath插件(适合python+xpath爬虫使用)

    里面有:火狐老版本的浏览器,xpath插件(适用于火狐),适合xpath爬虫的

    C# XML处理中xpath使用文档

    由于便捷的开发C#中xml相关的程序,欢迎下载使用

    Xpath使用实例简单粗暴.pdf

    Xpath使用实例简单粗暴.pdf

    xpath-helper for MAC

    Xpath helper 2.0.2,适用于MAC版, 提供的了压缩包,下载后,解压缩-打开chrome-&gt;更多工具-&gt;扩展程序-&gt;加载已经解压的扩展程序-&gt;找到解压的文件夹选中,此时会发现地址栏后面多了一个“X”的图标...Xpath就可以使用了

Global site tag (gtag.js) - Google Analytics