PHP XPath implementation of XML and HTML file fast parsing (with XML small database implementation of six-level word fast query instance)

Source: Internet
Author: User
Tags xpath

PHP XPath implementation of XML and HTML file fast parsing (with XML small database implementation of six-level word fast query instance)

First, XPath simple introduction

XPATH, XQUERY specifically queries XML language, fast query speed

How to use:

(1) Creating the DOM tool and loading the XML file

$xml = new DOMDocument (' 1.0 ', ' utf-8 ');

$xml-Load ('./dict.xml ');

Note: If you parse the HTML file quickly, the same reason

Use Loadhtmlfile (' dict.html ') here;

(2) Creating an XPath parsing tool

$xpath = new Domxpath ($xml);

(3) Writing an XPath query statement

$sql = '/dict/word[name= '. $word. ' "] /name ';

NOTE: If the HTML file is parsed quickly

$sql = '/html/body/div/h2 '; Get the contents under the H2 tag under the div below the body

(4) Query statement, return results

$words = $xpath->query ($sql);

Here are some syntax for the query statement table

Second, XML small database implementation of six-level word fast query instance

Preliminary version: Cons: Slow query speed

Receive the word and parse the XML query for the corresponding word

$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 implementation parsing and querying

$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/>

Improved version with XPath: fast speed

Usage examples:

$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);

Query Dict The following word, and the//name node under name= $word node

$sql = '/dict/word[name= '. $word. ' "] /name ';

$words = $xpath->query ($sql);

if ($words->length = = 0) {//If the word is not

echo "<br/>

Exit

}

To this step, the explanation found

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);

}

Attach the HTML file code:

 

The result is as follows:




Using XPath to quickly query an instance of an HTML file

$html = new DOMDocument (' 1.0 ', ' utf-8 ');

$html-Loadhtmlfile ('./dict.html ');

$xpath = new Domxpath ($html);

$sql = '/html/body/div/h2 '; Get the contents under the H2 tag under the div below the body

Echo $xpath->query ($sql)->item (0)->nodevalue;

PHP XPath implementation of XML and HTML file fast parsing (with XML small database implementation of six-level word fast query instance)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.