Php uses simplexml to parse xml

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.