PHP self-learning path ----- Basic Introduction to XML programming (Xpath technology, simpleXml technology. 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. you can get all the core ideas that conform to the XPath design. you can use xpath to quickly locate the node you want to search. 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 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]
// 1 create a DOMDocument object
$ Dom = new DOMDocument ("1.0", "UTF-8 ");
// 2 load the Xml file
$ Dom-> load ("simplexml. xml ");
// 3 convert to the DomXPath object
$ Xpath = new DOMXPath ($ dom );
// 4 search for jobs
$ Node_list = $ xpath-> query ("/books/book/title ");
Echo "a total of". $ 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 ."
";
}
?>
Enter the following information:
A total of 2
Title: tianlongbabu
Title: xiaoaojianghu
To learn more about the use of XPath,
SimpleXml
SimpleXMl: uses object-oriented methods to operate Xml files. SimpleXml is a simple method, which is usually used with XPath,
[Php]
// Loading Method 1: load an Xml file
// $ Lib = simplexml_load_file ("simplexml. xml ");
// 2 DOM object loading method
// 1 create a DOMDocument object
$ Dom = new DOMDocument ("1.0", "UTF-8 ");
// 2 load the Xml file
$ Dom-> load ("simplexml. xml ");
$ Lib = simplexml_import_dom ($ dom );
Echo"
"; // var_dump($lib);/*1*/
echo "
";
// Extract the book,
$ Books = $ lib-> book;
/* // Obtain the first book
$ Book1 = $ books [0];
// Retrieve the title
Echo $ book1-> title. ":". $ book1-> author ;*/
For ($ I = 0; $ I < p>
$ Book1 = $ books [$ I];
// Retrieve attributes
Echo "property:". $ book1 ['House']."
";
The title of echo ". $ I." Book | author
";
Echo $ book1-> title. "|". $ book1-> author ."
";
}
Echo "*** combination of simplexml and xpath ***
";
Echo "retrieve all the titles:
";
$ Titles = $ lib-> xpath ("// title ");
Foreach ($ titles as $ key => $ val ){
Echo"
". $ Key." | ". $ val;
}
Echo"
*********************************";
?>
The output is as follows:
[Php]
Property: qinghuachubanshe
Title of the 0th book | author
Tianlongbabu | xiaoming
Property: beidachubanshe
Title of the 1st book | author
Xiaojianghu | daming
* ** Combination of simplexml and xpath ***
Retrieve all titles:
0 | tianlongbabu
1 | xiaoaojianghu
*********************************
The third method for reprinting Xml is as follows:
[Php]
Echo"
Simplexml can also directly load a string (Xml specification )";
$ String = < < p>
Forty What?
Joe
Jane
I know that's the answer -- but what's the question?
XML;
$ Xml = simplexml_load_string ($ string );
Print_r ($ xml );
?>
Bytes. The main purpose is to describe the location of a node relative to other nodes and obtain all matching entries...