There is an XML file that reads as follows:
Copy Code code as follows:
<?xml version= "1.0"?>
<shows>
<show>
<name>Simpsons</name>
<channel>FOX</channel>
<start>8:00 pm</start>
<duration>30</duration>
</show>
<show>
<name>law & Order</name>
<channel>NBC</channel>
<start>8:00 pm</start>
<duration>60</duration>
</show>
</shows>
Here we use the XmlReader extension to parse and process the file, XmlReader another benefit is to read the data in the form of stream, that is, even in the face of large XML files can still be handled calmly. Here is how we deal with the above documents:
Copy Code code 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 ();