This article mainly introduces how to use xmlreader to read xml data in PHP. This example is relatively simple and only contains one reading function. For more information, see
This article mainly introduces how to use xmlreader to read xml data in PHP. This example is relatively simple and only contains one reading function. For more information, see
There is an XML file with the following content:
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 ();