`
neitheraaa
  • 浏览: 26695 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

xpath使用效率探索(转)

    博客分类:
  • java
阅读更多
                                       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());

    }

} 


本文来自CSDN博客,转载请标明出处:file:///C:/Documents%20and%20Settings/user/桌面/xpath使用效率探索%20-%20corry%20blog%20-%20CSDN博客.mht
分享到:
评论

相关推荐

    xpath-helper

    7. **便捷保存和分享**: 用户还可以保存常用的XPath表达式,以便于后续使用,或者分享给团队成员,提升团队协作效率。 8. **学习资源**: 除了作为实用工具,XPath Helper还可以作为一个学习XPath的辅助工具,通过...

    Xpath

    同时,`xpath util.txt` 和 `xpath.txt` 可能包含了XPath的实用工具或示例代码,可以进一步探索XPath的实际运用。通过学习XPath,开发者能够更高效地处理XML文档,提升数据处理的效率和灵活性。

    web xpath helper

    总之,Web XPath Helper是网页开发者和测试人员的强大工具,它简化了XPath的使用,提升了工作效率,同时通过直观的交互方式,使开发者更好地理解和掌握XPath这一技术。无论是进行网页元素定位,还是在自动化测试中...

    xpath应用学习小实验

    XPath,全称XML Path Language,是一种在XML文档中查找信息的语言。它被广泛应用于Python的Web抓取和自动化测试...不断实践和探索,你会发现XPath是XML和HTML处理的强大工具,能极大地提高Web抓取和自动化测试的效率。

    xpath-help插件

    总的来说,XPath-help插件通过提供直观的用户界面和强大的XPath支持,使得在Chrome浏览器中探索和操作网页内容变得轻而易举。只需简单的几步设置,你就可以充分利用XPath的强大功能,解决网页数据提取和分析中的各种...

    Python库 | xpath_string-0.0.2-py3-none-any.whl

    《Python库xpath_string-0.0.2-py3-none-any.whl的探索与应用》 在编程领域,Python以其简洁、易读的语法和强大的库支持,深受开发者喜爱。在处理XML数据时,XPath(XML Path Language)是一种强大的查询语言,而...

    爬虫技术-Xpath解析数据.pptx

    爬虫技术是网络数据抓取的重要手段,而Xpath则是爬虫中用于解析HTML或XML文档的关键技术。...通过不断实践和探索,我们可以灵活运用Xpath解决各种网页解析问题,从而更好地服务于数据分析和信息挖掘的需求。

    XpathTools.zip

    XPathTools是一款基于C#开发的实用工具,专为简化网页Xpath查找而设计。...通过"AutoXpathTools"这个主程序,用户可以轻松地在各种网页上探索和利用Xpath,提高工作效率,降低手动工作的繁琐程度。

    web端自动化基础xpath.docx

    - **提高效率**:自动化测试能够快速执行大量测试用例,节省人力成本。 - **增强准确性**:减少人为因素导致的错误,确保每次测试结果的一致性和可靠性。 - **持续集成/持续部署(CI/CD)**:自动化测试是CI/CD流程...

    CSS、DHTML、XMLDOM、T-SQL、XPath、正则表达式等.chm文件

    XPath使用路径表达式来选取节点,其语法简洁,且支持数字和字符串操作,常用于XSLT、XQuery等XML处理技术中。 6. 正则表达式:正则表达式是一种模式匹配工具,用于在字符串中搜索特定模式。它们广泛应用于文本编辑...

    LINQ简介及使用探索

    5. **LINQ to XML**:对XML文档进行查询和操作,提供了类似XPath和XQuery的功能。 【LINQ to SQL】 LINQ to SQL是.NET Framework的一部分,用于在对象模型和关系数据库之间建立映射,提供了一种声明性的方式来处理...

    xslt例子(转换修改xml)

    XSLT,全称eXtensible Stylesheet ...同时,也可以通过修改样例,进一步探索XSLT的更多特性,如模板的优先级、模式匹配的复杂性等。对于任何处理大量XML数据的开发者来说,掌握XSLT是提升效率和灵活性的重要技能。

    D-XMLManager:XML编辑器,用于使用XPath处理多个XML文件-开源

    D-XMLManager是一款专为IT专业人士设计的高效XML编辑器,它具有批量处理XML文件的能力,尤其在使用XPath进行查询和修改方面表现出色。这款工具的核心优势在于其对XPath的支持,使得用户能够便捷地定位并操作XML文档...

    Android 本地网络小说爬虫,基于jsoup及xpath.zip

    多任务处理:Android允许用户同时运行多个应用程序,并且可以轻松地在不同应用程序之间切换,提高了效率和便利性。 丰富的应用生态系统:Android系统拥有庞大的应用程序生态系统,用户可以从Google Play商店或其他第...

    IEXPath.zip

    XPath使用路径表达式来选取XML文档中的节点。路径表达式类似于文件系统中的路径,例如“/html/body/p”可以选取HTML文档中的所有段落元素。此外,XPath还提供了许多函数和运算符,使得我们可以进行更复杂的查询,...

    Automator中文教程

    在当今快节奏的工作环境中,提高效率和节省时间成为了每个Mac用户追求的目标。幸运的是,苹果公司为MacOS平台提供了一系列实用工具来...所以,不妨进一步探索这个工具,你将会发现更多能够提升你工作效率的方法和技巧。

    文本选择器

    文本选择器也支持XPath测试,使得非XML专业人士也能轻松探索和利用XML数据。 在提供的文件列表中,`HtmlAgilityPack.dll` 是一个.NET框架下的库,主要用于解析和操作HTML文档。Html Agility Pack库能够处理不规则或...

    libxml2_libxslt_test.7z

    《深入解析libxml2与libxslt:libxml2_libxslt_test代码探索》 libxml2和libxslt是两个在XML处理领域至关重要的开源库。libxml2主要用于XML文档的解析、验证和处理,而libxslt则是用于XSLT转换的库,它允许我们将...

    JSONataJSON查询和转换语言

    2. **路径语法** - JSONata使用类似XPath的路径语法来导航JSON对象结构。例如,`$.name`可以用来获取JSON对象中最外层的`name`属性值。 3. **过滤和选择** - 使用`[条件]`语法可以筛选数组中的元素。例如,`$....

    xalan-j_2_7_0-bin.zip

    Xalan-J 2.7.0是一款广泛使用的XSLT解析器,尤其对那些正在探索和学习XSLT(可扩展样式表语言转换)的用户来说,它是一个不可或缺的工具。在本文中,我们将深入探讨Xalan-J 2.7.0的功能、工作原理以及如何利用它进行...

Global site tag (gtag.js) - Google Analytics