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>"," ","<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文件代码:
结果如果所示:
三、使用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;