We can see that the results are in the format of objects and arrays, so that we can easily obtain the value of each element in XML. php uses simplexml to parse xml.
Php uses simplexml to parse xml
The code is as follows:
$xml = <<
#;/loc>
2013-06-13 01:20:01
always
1.0
#;/loc>
2013-06-13 01:20:01
always
0.8
XML;$simple = simplexml_load_string($xml);// $url = 'http://www.php230.com/baidu_sitemap1.xml';// $simple = simplexml_load_file($url);
Here we can check the format of $ simple:
print_r($simple);
SimpleXMLElement Object( [url] => Array ( [0] => SimpleXMLElement Object ( [loc] => # [lastmod] => 2013-06-13 01:20:01 [changefreq] => always [priority] => 1.0 ) [1] => SimpleXMLElement Object ( [loc] => # [lastmod] => 2013-06-13 01:20:01 [changefreq] => always [priority] => 0.8 ) ))
We can see that the results are in the format of objects and arrays, so that we can easily obtain the values of each element in XML.
foreach ($simple->url as $val){ print $val->loc;}
Here, the loc value of each item is output.
The above section describes how php uses simplexml to parse xml content. For more information, see PHP Chinese website (www.php1.cn )!