首页 > 数据库技术 > 详细

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

时间:2015-02-05 13:18:57      阅读:435      评论:0      收藏:0      [点我收藏+]

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;             

 

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

原文:http://www.cnblogs.com/lihaiyan/p/4274347.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!