Load external xml file?

前端 未结 3 1549
粉色の甜心
粉色の甜心 2021-01-13 10:00

I have the following code (from a previous question on this site) which retrieves a certain image from an XML file:



        
3条回答
  •  误落风尘
    2021-01-13 10:28

    Procedurally, simple_xml_load_file.

    $file = '/path/to/test.xml';
    if (file_exists($file)) {
        $xml = simplexml_load_file($file);
        print_r($xml);
    } else {
        exit('Failed to open '.$file);
    }
    

    You may also want to consider using the OO interface, SimpleXMLElement.

    Edit: If the file is at some remote URI, file_exists won't work.

    $file = 'http://example.com/text.xml';
    if(!$xml = simplexml_load_file($file))
      exit('Failed to open '.$file);
    print_r($xml);
    

提交回复
热议问题