Why should we use XPATH? The closer the query in the previous blog is to the following words, the longer the query time will be, the less time it will be. XPAth is used to improve the XML parsing speed. Html can also be parsed, and the efficiency is also good! Code for querying the following information: vcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQbrush: SQL ;? For more information about php, see w3.
Why should we use XPATH? The closer the query in the previous blog is to the following words, the longer the query time will be, the less time it will be. XPAth is used to improve the XML parsing speed. Html can also be parsed, and the efficiency is also good! Code for querying the following information: vcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: SQL;">? Php // For details, refer to w3
Why should we use XPATH? The closer the query in the previous blog is to the following words, the longer the query time will be, the less time it will be. XPAth is used to improve the XML parsing speed. Html can also be parsed, and the efficiency is also good!
Query the following information separately
Code: <喎?http: www.2cto.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: SQL;"> Load ('book. xml '); $ xpath = new DOMXPATH ($ xml);/* $ SQL = 'xxx'; // path expression $ xpath-> query ($ SQL ); * // * how to write the path expression of xpath? Xpath is the path from the root node to a node * // query book. the title of each book under xml // bookstore/book/title/* $ SQL = '/bookstore/book/title '; $ rs = $ xpath-> query ($ SQL); print_r ($ rs); echo $ rs-> item (1)-> nodeValue; * // query book. where are the 2nd title nodes under the book node under xml? In this case, it is wrong to write/* $ SQL = '/bookstore/book/title [2]'; $ rs = $ xpath-> query ($ SQL ); print_r ($ rs-> length); * // query the title node under the 2nd book in bookestore. /* $ SQL = '/bookstore/book [2]/title'; $ rs = $ xpath-> query ($ SQL); print_r ($ rs-> item (0) -> nodeValue); * // query the book nodes under the bookstore at a price of more than 40 RMB/* $ SQL = '/bookstore/book [price> 40]/title '; $ rs = $ xpath-> query ($ SQL); echo $ rs-> item (0)-> nodeValue; * /// query the prices of Aegis /// bookstore/book below, and the price of the book with title = ' '$ SQL ='/bookstore/book [title = ""]/price '; $ rs = $ xpath-> query ($ SQL); echo $ rs-> item (0)-> nodeValue;
How does xpath query a node without considering the path hierarchy?
For example, we strictly query/bookstore/book/title
Now we add one,
Load ('book. xml '); $ xpath = new DOMXPATH ($ xml); $ SQL ='/bookstore/book [last ()]/title '; $ rs = $ xpath-> query ($ SQL); // only the title of the title can be found. // echo $ rs-> item (0)-> nodeValue; // think, how can I query all titles without considering hierarchies? $ SQL = '/title'; // This does not work. In this case, the title under the root node is queried, and the title/*/a/B under the root node is not found, a and B are parent-child relationships. If/a/B is used, it means that a is only the ancestor of B and the hierarchy is ignored * // not hierarchical, locate all title/* $ SQL = '// title'; foreach ($ xpath-> query ($ SQL) as $ v) {echo $ v-> nodeValue ,'
';} * // * $ SQL =' // title [2] '; //ABQuery all adjacent title nodes, and 2nd foreach ($ xpath-> query ($ SQL) as $ v) {echo $ v-> nodeValue ,'
';}*/
The above is a simple application to improve the efficiency of the previous blog
Load ('. /dict. xml ');/* $ namelist = $ xml-> getElementsByTagName ('name'); $ isfind = false; foreach ($ namelist as $ v) {if ($ v-> nodeValue = $ word) {// print_r ($ v); echo $ word ,'
'; Echo' means: ', $ v-> nextSibling-> nodeValue ,'
'; Echo' example: ', $ v-> nextSibling-> nodeValue ,'
'; $ Isfind = true; break;} if (! $ Isfind) {echo 'sorry';} * // use xpath to query the dictionary $ xpath = new DOMXpath ($ xml); // query the word in/dict, and the/name node under the node name = $ word $ SQL = '/dict/word [name = "'. $ word. '"]/name'; // echo $ SQL; $ words = $ xpath-> query ($ SQL); if ($ words-> length = 0) {echo 'sorry'; exit;} // $ name = $ words-> item (0); echo $ word ,'
'; Echo' means: ', $ name-> nextSibling-> nodeValue ,'
'; Echo' example: ', $ name-> nextSibling-> nodeValue ,'
';
To parse the html
Loadhtmlfile('dict.html '); $ xpath = new DOMXPATH ($ html); $ SQL ='/html/body/h2 '; echo $ xpath-> query ($ SQL) -> item (0)-> nodeValue ,'
'; // Query the p node of id = "abc" $ SQL =' // p [@ id = "abc"] '; echo $ xpath-> query ($ SQL) -> item (0)-> nodeValue; // analyze the content of 2nd adjacent spans under 2nd/p/$ SQL = '// p/span [2]'; echo $ xpath-> query ($ SQL)-> item (0)-> nodeValue;