?php// 详细学习可以参考w3"/> ?php// 详细学习可以参考w3">
Home > Database > Mysql Tutorial > XPath快速解析XML

XPath快速解析XML

WBOY
Release: 2016-06-07 16:10:29
Original
1288 people have browsed it

为什么要使用XPATH,上一篇博客查询越靠近下面单词,时间会越长,超过2s就不太好了,XPAth就是用来提高解析XML速度的。还可以解析html,效率也是不错的! 分别查询下列信息 代码: vcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:sql;">?php// 详细学习可以参考w3

为什么要使用XPATH,上一篇博客查询越靠近下面单词,时间会越长,超过2s就不太好了,XPAth就是用来提高解析XML速度的。还可以解析html,效率也是不错的!

分别查询下列信息

\

代码:喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:sql;">load('book.xml'); $xpath = new DOMXPATH($xml); /* $sql = 'xxx'; // 路径表达式 $xpath->query($sql); */ /* xpath的路径表达式如何写? xpath是从根节点到某个节点声经过的路径 */ // 查询book.xml下面的每本书的title // /bookstore/book/title /* $sql = '/bookstore/book/title'; $rs = $xpath->query($sql); print_r($rs); echo $rs->item(1)->nodeValue; */ // 查询book.xml下面book节点的下面的第2个title节点,哪来的第2个title节点? 这样写是不对的 /* $sql = '/bookstore/book/title[2]'; $rs = $xpath->query($sql); print_r($rs->length); */ // 查询bookestore下面的第2本书下面的title节点. /* $sql = '/bookstore/book[2]/title'; $rs = $xpath->query($sql); print_r($rs->item(0)->nodeValue); */ // 查询bookstore下面的book节点并且价格>40元 /* $sql = '/bookstore/book[price>40]/title'; $rs = $xpath->query($sql); echo $rs->item(0)->nodeValue; */ // 查询侠客行的价格 // /bookstore/下面的book,且title=='侠客行'的书的价格 $sql = '/bookstore/book[title="侠客行"]/price'; $rs = $xpath->query($sql); echo $rs->item(0)->nodeValue;
xpath如何不考虑路径的层次,来查询某个节点


比如我们刚才严格层次查询 /bookstore/book/title
现在我们加了一个,

<?php
$xml = new DOMDocument(&#39;1.0&#39;,&#39;utf-8&#39;);
$xml->load(&#39;book.xml&#39;);

$xpath = new DOMXPATH($xml);

$sql = &#39;/bookstore/book[last()]/title&#39;;
$rs = $xpath->query($sql);

// 只能查到书名的title
//echo $rs->item(0)->nodeValue; 


// 思考 ,如何查询所有的title,不考虑层次关系?
$sql = &#39;/title&#39;; // 这样不行,这样查的是根节点下的title,而根节点下没有title

/*
/a/b,这说明,a,b就是父子关系,而如果用/a//b,这样说明a只是b的祖先就行,忽略了层次
*/


// 不分层次,查出所有的title
/*
$sql = &#39;//title&#39;;
foreach($xpath->query($sql) as $v) {
    echo $v->nodeValue,&#39;<br />&#39;;
}
*/

/*
$sql = &#39;//title[2]&#39;; // 这样又理解成<title>a</title><title>b</title>,查询所有相邻的title节点,且第2个
foreach($xpath->query($sql) as $v) {
    echo $v->nodeValue,&#39;<br />&#39;;
}
*/
Copy after login

上面是简单应用,来改善上篇博客效率问题

<?php
// 接收单词并解析XML查询相应的单词
$word = isset($_GET[&#39;word&#39;])?trim($_GET[&#39;word&#39;]):&#39;&#39;;

if(empty($word)) {
    exit(&#39;你想查啥?&#39;);
}


// 解析XML并查询
$xml = new DOMDocument(&#39;1.0&#39;,&#39;utf-8&#39;);
$xml->load(&#39;./dict.xml&#39;);


/*
$namelist = $xml->getElementsByTagName(&#39;name&#39;);

$isfind = false;

foreach($namelist as $v) {
    if($v->nodeValue == $word) {
        //print_r($v);
        echo $word,&#39;<br />&#39;;
        echo &#39;意思:&#39;,$v->nextSibling->nodeValue,&#39;<br />&#39;;
        echo &#39;例句:&#39;,$v->nextSibling->nextSibling->nodeValue,&#39;<br />&#39;;

        $isfind = true;
        break;
    }
}

if(!$isfind) {
    echo &#39;sorry&#39;;
}
*/






// 接下来用xpath来查询词典
$xpath = new DOMXpath($xml);

// 查询/dict下的word,且name=$word的节点下面的/name节点
$sql = &#39;/dict/word[name="&#39; . $word . &#39;"]/name&#39;; 
//echo $sql;
$words = $xpath->query($sql);

if($words->length == 0) {
    echo &#39;sorry&#39;;
    exit;
}


// 查到了
$name = $words->item(0);
echo $word,&#39;<br />&#39;;
echo &#39;意思:&#39;,$name->nextSibling->nodeValue,&#39;<br />&#39;;
echo &#39;例句:&#39;,$name->nextSibling->nextSibling->nodeValue,&#39;<br />&#39;;
Copy after login

来解析一下的html

<?php
/***
====笔记部分====
xpath是根据DOM标准来查询,
html也是DOM,
也能查,岂只是xml
***/


$html = new DOMDocument(&#39;1.0&#39;,&#39;utf-8&#39;);
$html->loadhtmlfile(&#39;dict.html&#39;);


$xpath = new DOMXPATH($html);
$sql = &#39;/html/body/h2&#39;;
echo $xpath->query($sql)->item(0)->nodeValue,&#39;<br />&#39;;


// 查询id="abc"的div节点
$sql = &#39;//div[@id="abc"]&#39;;
echo $xpath->query($sql)->item(0)->nodeValue;


// 分析第2个/div/下的p下的相邻span的第2个span的内容
$sql = &#39;//div/p/span[2]&#39;;
echo $xpath->query($sql)->item(0)->nodeValue;
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template