PHP self-study road-----Basic introduction to XML programming (Xpath technology, simpleXml technology)_PHP tutorial
WBOY
Release: 2016-07-15 13:21:51
Original
1241 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,
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);
?>
http://www.bkjia.com/PHPjc/477134.htmlwww.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...
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