This article describes how to use xmlreader to read xml data in PHP. the example in this article is relatively simple and only contains one reading function. For more information, see the following XML file:
The code is as follows:
<? Xml version = "1.0"?>
Simpsons
FOX
8: 00 PM
30
Law & Order
NBC
8: 00 PM
60
Here we use XMLReader extension to parse and process the file. XMLReader also has the advantage of reading data in the form of Stream, that is, even if the xml file is large, it can still be processed with ease. The following describes how to process the above files:
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 ();