PHP self-learning path ----- Basic Introduction to XML programming (Xpath technology, simpleXml technology)

Source: Internet
Author: User
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);?>

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.