• PHP中XPATH 实现xml及html文件快速解析(附xml做小型数据库实现六级单词快速查询实例)


    PHP中XPATH 实现xml及html文件快速解析(附xml做小型数据库实现六级单词快速查询实例)

    一、XPATH简单入门

    XPATH ,XQUERY 专门查询XML语言,查询速度快

    使用方法:

    (1)创建DOM工具,并且加载 xml文件

           $xml = new  DOMDocument('1.0','utf-8');

           $xml -> load('./dict.xml');             

    注:若是对html文件进行快速解析, 同样的道理

           此处使用   loadhtmlfile (‘dict.html’);

    (2)创建xpath解析工具

           $xpath = new DOMXPATH($xml);      

    (3)编写xpath查询语句

           $sql = '/dict/word[name="'.$word.'"]/name';

    注:若是对html文件进行快速解析

           $sql = ‘/html/body/div/h2’;              //得到body下的div下面的h2标签下的内容

    (4)查询语句,返回结果

    $words = $xpath->query($sql); 

    下面是查询语句表的一些语法

    二、xml做小型数据库实现六级单词快速查询实例

    初步版本:缺点:查询速度慢

    //接收单词并解析XML查询相应的单词

    $word = isset($_GET['word'])?trim($_GET['word']):'';

    if(empty($word)){

           exit("I am sorry,I can't catch the word!");

    }else{

           $xml = new  DOMDocument('1.0','utf-8');

           $xml -> load('./dict.xml');

    // * DOM实现解析并查询

           $namelist = $xml->getElementsByTagName('name');

           $value = array();

           $isfind = false;

           foreach($namelist as $v){

                  if(($v->nodeValue) == $word){

                         //print_r($v);

                         echo $word,"<br/>";

                         echo "Meaning:",$v->nextSibling->nodeValue,"<br/>";

                         echo "Example:",$v->nextSibling->nextSibling->nodeValue,"<br/>";               

      //$value['Word'] = $word;

    //$value['Meaning'] = $v->nextSibling->nodeValue;

    //$value['Example'] = str_replace('/r/n',"<br/>",$v->nextSibling->nextSibling->nodeValue);     

                         $isfind = true;

                         break;

                  }

           }

           if(!$isfind)

                  echo "<br/><h3>Sorry,the word is not in the directory</h3>";

    使用XPATH改进版本:速度快

    使用实例:

    $word = isset($_GET['word'])?trim($_GET['word']):'';

    if(empty($word)){

           exit("I am sorry,I can't catch the word!");

    }else{

    $xml = new  DOMDocument('1.0','utf-8');

    $xml -> load('./dict.xml');

    $xpath = new DOMXPATH($xml);                    

    //查询dict下面的word,并且name=$word的节点 下的//name节点

    $sql = '/dict/word[name="'.$word.'"]/name';

    $words = $xpath->query($sql);

    if($words->length == 0){  //如果没有这个单词

                  echo "<br/><h3>Sorry,the word is not in the directory</h3>";

                  exit;

    }

                  //走到这一步,说明查到了

    Echo  "<b>Word:</b>","&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;","<B>$word

    </B>","<br/>";

    echo "<b>Meaning:</b>",$name->nextSibling->nodeValue,"<br/>";

    echo "<b>Example:</b>",$name->nextSibling->nextSibling->nodeValue,"<br/>";

    //$value['Word'] = $word;

    //$value['Meaning'] = $name->nextSibling->nodeValue;

    //$value['Example'] = str_replace('/r/n',"<br/>",$name->nextSibling->nextSibling->nodeValue); 

    }

    附上HTML文件代码:
     

     
    <html>
            <head>
            <meta http-equiv="content-type" content="text/html; charset=UTF-8">
            <title>dict</title>
            </head>
            <style>
                //style="text-align:center;"}
            </style>
            <body>
                    <div >
                    <h2>XML做小型数据库实现在线词典</h2>
                        <!--        <form action="dict.php">   -->
                    <form>
                        <p>单词:<input type='text' name='word'/></p>
                        <p><input type="submit" value="查询"/></p>  
                    </form>
                    <p> 
                    <?PHP if(isset($value['Word'])) {
                           foreach($value as $k=>$v){  ?>
                                <b ><?php echo $k.": ";?></b>
                                <p ><?php echo $v;?></p>
                                <br/>
                    <?php
                          }
                    }?>     
                    </p>
            </div>
        </body>
    </html>
     

    结果如果所示:
    图片 

     


     

    三、使用XPATH快速查询html文件实例

    $html = new  DOMDocument('1.0','utf-8');

    $html -> loadhtmlfile('./dict.html');

    $xpath= new DOMXPATH($html);                    

    $sql = ‘/html/body/div/h2’;        //得到body下的div下面的h2标签下的内容

    Echo $xpath->query($sql)->item(0)->nodeValue;             

  • 相关阅读:
    SpringMVC中@Controller和@RequestMapping用法和其他常用注解
    eclipse maven install 时控制台乱码问题解决
    使用模板实现编译期间多态(类名当参数)
    QT中Dialog的使用(使用QStackedWidget维护页面切换)
    QT中的各种对话框
    Qt 5 最小构建笔记(只编译QtBase)
    忽然懂了:“视图”的用途不仅仅是临时表,更适用于变化比较大的情况,而且不用改客户端一行代码
    React-Native
    一位OWin服务器新成员TinyFox
    Access Toke调用受保护的API
  • 原文地址:https://www.cnblogs.com/lihaiyan/p/4274347.html
Copyright © 2020-2023  润新知