0

有这样的代码:

if (file_exists('my.xml')) {
    $xml = simplexml_load_file('my.xml');
    if($xml === NULL)
        echo('ERR NULL $xml</br>');
    else{
        echo('> my.xml opened</br>');   
        print_r($xml);
    }
}

foreach($xml->data->record as $records){
    echo $records->data_to_retrive, PHP_EOL;
}
echo('> Ended</br');

现在,输出如下所示:

> my.xml opened
> Ended

那有什么问题呢?为什么它t show data_to_retrive, and print_r($xml) don不起作用?我卡在这里,差点头疼。

4

2 回答 2

0

simplexml_load_filefalse如果失败则返回===与返回值进行比较,NULL因为false === NULL将失败。您可以使用

if($xml === false)

反而。

于 2013-09-23T00:44:41.223 回答
0
$file = fopen('my.xml','r');
$prexml = fread($file, filesize('my.xml'));
$prexml = preg_replace('#<([0-9]+)>#si','<z$1>',$prexml);
$prexml = preg_replace('#</([0-9]+)>#si','</z$1>',$prexml);
echo('> File opened</br>');
$xml = new SimpleXMLElement($prexml);

好的,我以这种方式修复了 my.xml,现在它可以工作了。感谢杨的想法!

于 2013-09-23T12:28:37.783 回答