zoukankan      html  css  js  c++  java
  • 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;             

  • 相关阅读:
    数组的排序
    2017-2018学年实习心得
    2017-2018学年实习总结
    古人警句
    课程意见
    第二次冲刺第十天
    第二次冲刺第九天
    第二次冲刺第八天
    第二天冲刺第七天
    第二次冲刺第六天
  • 原文地址:https://www.cnblogs.com/lihaiyan/p/4274347.html
Copyright © 2011-2022 走看看