这个“非常简单”的单形 scrip't 不起作用


This "really simple" simplexml scrip't isn't working

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<pages>
<page>
    <title>Home</title>
    <content>Lorem Ipsum</content>
</page>
<page>
    <title>Pictures</title>
    <content>Lorem Ipsum</content>
</page>
<page>
    <title>Information</title>
    <content>Lorem Ipsum</content>
</page>
</pages>

我的 XML

$url = "xml->build.xml";
$xml = simplexml_load_file($url);
$i = 1;
foreach($xml->page as $page) {
    echo "<strong>Page ".$i.":</strong> ".$page->title."<br/>";
    $i++;
}

我的菲律宾比索

我得到:

警告:simplexml_load_file():I/O 警告:无法加载外部实体"xml->build.xml"

就从那个简单的脚本!有什么想法吗?:)

更新,这是确切的XML文件

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<pages>
<page>
    <title>Home</title>
    <content>Lorem Ipsum</content>
</page>
<page>
    <title>Pictures</title>
    <content>Lorem Ipsum</content>
</page>
<page>
    <title>Information</title>
    <content>Lorem Ipsum</content>
</page>
</pages>
<css>
<css-tag>
    <title>background-color</title>
    <value>#FFF</value>
</css-tag>
</css>
<layout>1</layout>

发生这种情况是因为您尝试读取不存在的文件。如果您的文件名为 build.xml 并且存储在与 PHP 脚本相同的目录中,请替换:

$url = "xml->build.xml";

跟:

$url = dirname(__FILE__) . "/build.xml";

不确定为什么会出现这样的错误,但这可能有助于您解决它

我认为xml->build.xml也不是一个有效的名字,你也可以把它改成build.xml

尝试

$url = "build.xml";
if(!is_file($url))
    die("File Does not exist");
if(!is_readable($url))
    die("File is not readeable");

$xml = simplexml_load_string(file_get_contents($url));
var_dump($xml);
$xml = simplexml_load_file($url);
var_dump($xml);

如果simplexml_load_string肯定有效simplexml_load_file也会起作用

更新

您的XML错误,格式应如下所示

$url = '<?xml version="1.0" encoding="ISO-8859-1" ?> 
<doc>
<pages>
<page>
    <title>Home</title>
    <content>Lorem Ipsum</content>
</page>
<page>
    <title>Pictures</title>
    <content>Lorem Ipsum</content>
</page>
<page>
    <title>Information</title>
    <content>Lorem Ipsum</content>
</page>
</pages>
<css>
<css-tag>
    <title>background-color</title>
    <value>#FFF</value>
</css-tag>
</css><layout>1</layout>
</doc>';