PHP self-study road-----Basic introduction to XML programming (Xpath technology, simpleXml technology)_PHP tutorial

WBOY
Release: 2016-07-15 13:21:51
Original
1240 people have browsed it

The core idea of ​​XPath's design is that you can quickly and briefly locate the node you want to find through xpath. The main purpose is to describe the position of a node relative to other nodes. All nodes that meet the conditions can be obtained and become [position path].

Xapth is mainly used to query information in XML documents. It can parse XML files and read data in XML files by using path expressions
The following is a case introduction. The XML file is as follows (simplexml.xml):
[php]
sb001
200
xiaoming
tianlongbabu
sb002
400
daming
xiaoaojianghu
The PHP code is as follows:
[php]
//1 Create DOMDocument object
$dom = new DOMDocument("1.0", "UTF-8");
//2 Load Xml file
$dom->load("simplexml.xml");
//3 Convert to DomXPath object
$xpath = new DOMXPath($dom);
//4 Find a job
$node_list = $xpath->query("/books/book/title");
echo "There are ".$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:
There are 2 in total
title:tianlongbabu
title:xiaoaojianghu
If you want to know more about the use of XPath,
simpleXml
The core idea of ​​simpleXMl: use an object-oriented method to operate Xml files. simpleXml is a relatively simple method, usually used in conjunction with XPath,
[php]
//Loading method 1 ‘Load an Xml file
// $lib = simplexml_load_file("simplexml.xml");
//2 DOM object loading method
//1 Create DOMDocument object
$dom = new DOMDocument("1.0", "UTF-8");
//2 Load Xml file
$dom->load("simplexml.xml");
$lib = simplexml_import_dom($dom);
echo "
"; </div>
<div> // var_dump($lib);/*1*/ </div>
<div> echo "
";
//Take out the book,
$books = $lib->book;
/* //Take out the first book
$book1=$books[0];
//Get the book title
echo $book1->title.":".$book1->author; */
for($i=0;$i
$book1=$books[$i];
//Get attributes
echo "Attribute:".$book1['house']."
";
echo "Title of the book ".$i."||Author
";
echo $book1->title."||".$book1->author."
";
}
echo "***simplexml and xpath combined***
";
echo "Get all book titles:
";
$titles = $lib->xpath("//title");
foreach ($titles as $key=>$val){
echo "
".$key."||".$val;
}
echo "
************************************";
?>
The output is as follows:
[php]
Attribute: qinghuachubanshe
Title of Book 0||Author
tianlongbabu||xiaoming
Attribute: beidachubanshe
Title of Book 1||Author
xiaoaojianghu||daming
***simplexml and xpath combined***
Retrieve all book titles:
0||tianlongbabu
1||xiaoaojianghu
**********************************
The third way to reprint Xml, string mode, is as follows:
[php]
echo "
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);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477134.htmlTechArticleThe core idea of ​​XPath’s design is that you can quickly and briefly locate the node you want to find through xpath. The main purpose is to describe the position of a node relative to other nodes, and all matching conditions can be obtained...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template