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>", " ", "<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)