PHP Self-study-----The basics of XML programming (XPath technology, SimpleXML Technology) _php Tutorial

Source: Internet
Author: User
The core idea of XPath design can be quickly introduced by XPath to the node you want to locate. The main purpose is to describe the position of the node relative to the other nodes, and can get all the nodes that meet the criteria and become the [location path]. Xapth is mainly used to query information in the XML document tool, by using the path expression can parse the XML file, read the data in the XML file is described below as a case, the XML file is as follows (simplexml.xml): [PHP] sb001 xiaoming Tianlongbabu sb002 Daming Xiaoaojianghu The PHP code is as follows: [PHP] Load ("Simplexml.xml"); 3 turn into Domxpath object $xpath = new Domxpath ($dom); 4 find work $node _list = $xpath->query ("/books/book/title"); echo "Total". $node _list->length. " A
"; 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. "
"; } ? > enter as follows: A total of 2 Title:tianlongbabutitle:xiaoaojianghu want to know more about the use of XPath, simpleXml SimpleXml's core idea: to manipulate XML files in an object-oriented way. SimpleXML is a relatively simple way to use with XPath, [PHP] Load ("Simplexml.xml"); $lib = Simplexml_import_dom ($dom); echo "
";     Var_dump ($lib);/*1*/      echo "
"; Take out the book, $books = $lib->book; /*//Take out the first book $book 1= $books [0]; Take out the title echo $book 1->title. ":" $book 1->author; */for ($i =0; $i
"; echo "First". $i. " The title of the book | | Author
"; echo $book 1->title. "| |". $book 1->author. "
"; } echo "***simplexml and XPath used in conjunction * * *
"; echo "takes out all the titles:
"; $titles = $lib->xpath ("//title"); foreach ($titles as $key = + $val) {echo "
". $key." | | ". $val; } echo "
The?> output is as follows: [PHP] Properties: Qinghuachubanshe The title of the No. 0 book | | Author Tianlongbabu| | Xiaoming Properties: The title of the 1th Book of Beidachubanshe | | Author Xiaoaojianghu| | Daming ***simplexml and XPath use * * * To remove all titles: 0| | Tianlongbabu 1| | Xiaoaojianghu ********************************* The Third kind of reprint XML way, String way, as follows: [PHP] SimpleXML can also be loaded directly with a string string (XML specification) "; $string = << Forty what? Joe Jane I know that ' s the answer--and what's the ' s the question? XML; $xml = simplexml_load_string ($string); Print_r ($xml);?>

http://www.bkjia.com/PHPjc/477134.html www.bkjia.com true http://www.bkjia.com/PHPjc/477134.html techarticle The core idea of XPath design can be quickly introduced by XPath to the node you want to locate. The main purpose is to describe the position of the node relative to the other nodes and can get all the compliance bars ...

  • Related Article

    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.