【发布时间】:2010-07-28 09:46:41
【问题描述】:
我是 PHP 的初学者,我对 XML 操作一无所知。我正在研究如下所示的 Google CSE 注释 XML:
<?xml version="1.0" encoding="UTF-8" ?>
- <Annotations>
- <Annotation about="http://sanspace.in/">
<Label name="_cse_byxamvbyjpc" />
</Annotation>
- <Annotation about="http://blog.sanspace.in/">
<Label name="_cse_byxamvbyjpc" />
</Annotation>
- <Annotation about="http://google.com/">
<Label name="_cse_exclude_byxamvbyjpc" />
</Annotation>
</Annotations>
我想从上面显示的文件中实现这一点:
<?xml version="1.0" encoding="UTF-8" ?>
- <Annotations>
- <Annotation about="http://sanspace.in/">
<Label name="testString1" />
</Annotation>
- <Annotation about="http://blog.sanspace.in/">
<Label name="testString2" />
</Annotation>
- <Annotation about="http://google.com/">
<Label name="testString2" />
</Annotation>
</Annotations>
到目前为止,我已经尝试过:
<?php
if (file_exists('test.xml'))
{
$xml = simplexml_load_file('test.xml');
}
else
{
exit('Error.');
}
foreach($xml->Annotation as $annotation)
{
if ($annotation["about"]=="http://sanspace.in/")
{ $annotation->Label["name"]="testString1"; }
else
{ $annotation->Label["name"]="testString2"; } }
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
echo $dom->saveXML();
$dom->save("test.xml");
?>
此代码执行任务,但不会将其保存到文件中。
我的问题是,$dom->save("test.xml"); 语句有什么问题?
如何在服务器上保存 XML 文件?
【问题讨论】:
-
@Gordon,编辑了问题。
-
@San 除了第二个 if 语句中缺少括号外,它是否有效?有什么错误吗?
-
@Gordon,我添加了括号。没有变化。当我通过浏览器调用这个文件时,我只是得到一个空白页。 test.xml 文件位于此 php 页面所在的同一目录中。我做错什么了吗?
-
@San 查看页面的源代码。也是空白吗?如果是这样,请启用
error_reporting(-1);并确保您的 php.ini 启用了display_errors和display_startup_errors。然后重新加载页面,看看它是否有任何错误。 -
@Gordon 它是空白的。但是,除了该文件上的 php 代码外,什么都没有。因此,即使它工作正常,它也会是空白的。那正确吗?我明白你关于错误的观点。但是,我不知道我在哪里启用错误报告以及 php.ini 的位置。我使用 cPanel 和 PHP5(我希望)。