The core idea of XPath design can be quickly introduced by xpath to locate the desired node. The main purpose is to describe the location of a node relative to another node. all qualified nodes can be obtained and become [location paths]. Xapth is mainly used to query information in XML documents... "/> <scripttype =" text/javascript "src =" http: // The core idea of XPath design, you can quickly locate the node you want to search for through xpath. The main purpose is to describe the location of a node relative to another node. all qualified nodes can be obtained and become [location paths]. Xapth is mainly used to query information in XML documents. you can use path expressions to parse XML files and read the data in XML files. The following is a case study. the XML file is as follows (simplexml. xml): [php]
sb001
200 Xiaoming Tianlongbabu
sb002
400 Daming Xiaojianghu The PHP code is as follows: [php] Load ("simplexml. xml "); // 3 convert to the DomXPath object $ xpath = new DOMXPath ($ dom ); // 4 search for work $ node_list = $ xpath-> query ("/books/book/title"); echo "total ". $ node_list-> length."
"; For ($ I = 0; $ I <$ node_list-> length; $ I ++) {$ node = $ node_list-> item ($ I ); // var_dump ($ node_list);/* foreach ($ node as $ key => $ val) {echo} */echo $ node-> nodeName. ":". $ node-> nodeValue."
";}?> The input is as follows: there are two titles: tianlongbabutitle: xiaojianghu. to learn more about the use of XPath, simpleXml simpleXMl's core idea is to operate Xml files using object-oriented methods. SimpleXml is a simple method. it is usually used with XPath. [php] Load ("simplexml. xml"); $ lib = simplexml_import_dom ($ dom); echo"
"; // var_dump($lib);/*1*/ echo "
"; // Retrieve the book, $ books = $ lib-> book;/* // retrieve the first book $ book1 = $ books [0]; // Retrieve the title echo $ book1-> title. ":". $ book1-> author; */for ($ I = 0; $ I "; Echo". $ I. "Book title | author
"; Echo $ book1-> title." | ". $ book1-> author ."
";} Echo" *** combination of simplexml and xpath ***
"; Echo:
"; $ Titles = $ lib-> xpath (" // title "); foreach ($ titles as $ key => $ val) {echo"
". $ Key." | ". $ val;} echo"
********************************* ";?> Output: [php] attribute: Title of qinghuachubanshe 0th | author: tianlongbabu | xiaoming attribute: beidachubanshe: name of the 1st book | author: xiaojianghu | daming ***** simplexml and xpath used together *** to retrieve all the titles: 0 | tianlongbabu 1 | xiaoaojianghu ******************************** * The third method for Xml reprinting, the string method is as follows: [php] Simplexml can also directly load a string (Xml specification) "; $ string = < Forty What? Joe Jane I know that's the answer -- but what's the question? XML; $ xml = simplexml_load_string ($ string); print_r ($ xml);?>