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();
http://www.bkjia.com/PHPjc/934935.htmlwww.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/...