---
title: "xpath使用效率探索"
author: "zero_dian"
date: "2008-01-02T17:32:00Z"
tags: []
slug: "xpath"
draft: false
lastmod: "2008-01-02T17:32:00Z"
id: 442639
url: "https://zero-dian.iteye.com/blog/442639"
---

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()&lt;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());

}

}