Home  >  Article  >  Backend Development  >  Example of using xmlreader to read xml data in PHP, xmlreaderxml_PHP tutorial

Example of using xmlreader to read xml data in PHP, xmlreaderxml_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:10:371016browse

Example of using xmlreader to read xml data in PHP, xmlreaderxml

There is an XML file with the following content:

Copy code The code is as follows:




                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 





Here we use the XMLReader extension to parse and process the file. Another advantage of XMLReader is that it reads data in the form of Stream, that is, even if it is faced with an extremely large xml file, it can still be processed calmly. Here’s what we do with the above files:




Copy code
The code is as follows:

$indexUrl='http://www.xxx.com/xxx.xml';

$reader = new XMLReader(); $reader->open($indexUrl); $countElements = 0; while ($reader->read()){
If($reader->nodeType == XMLReader::ELEMENT){
$nodeName = $reader->name;
                                                                                 If($reader->nodeType == XMLReader::TEXT && !empty($nodeName)){
switch($nodeName){
case 'name':
$name = $reader->value;
break;
case 'channel':
$channel = $reader->value;
break;
case 'start':
$start = $reader->value;
break;
case 'duration':
$duration = $reader->value;
break;
                                                                                                                                                                             } 
$reader->close();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/934935.htmlTechArticleExample of using xmlreader to read xml data in PHP. xmlreaderxml has an XML file with the following content: Copy the code and the code is as follows: xml version="1.0" shows show nameSimpsons/name channelFOX/...
Statement:
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