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;             

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。