PHP uses simplexml to parse xml

黄舟
Release: 2017-02-24 15:29:43
Original
2096 people have browsed it

php uses simplexml to parse xml

PHP uses simplexml to parse xml

code show as below:


$xml = <<

    
        //m.sbmmt.com/;/loc>
        2013-06-13 01:20:01
        always
        1.0
    
    
        //m.sbmmt.com/;/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);
Copy after login

Here we can check the format of $simple:



print_r($simple);
Copy after login
SimpleXMLElement Object
(
    [url] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [loc] => //m.sbmmt.com/
                    [lastmod] => 2013-06-13 01:20:01
                    [changefreq] => always
                    [priority] => 1.0
                )
            [1] => SimpleXMLElement Object
                (
                    [loc] => //m.sbmmt.com/
                    [lastmod] => 2013-06-13 01:20:01
                    [changefreq] => always
                    [priority] => 0.8
                )
        )
)
Copy after login

We can see that the result is in the format of an object or array, so that we can easily obtain the value of each element in XML



foreach ($simple->url as $val){
    print $val->loc;
}
Copy after login

The loc value of each item will be output here.          

The above is the content of PHP using simplexml to parse xml. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!