我试图将xml文件中的注释添加到特定的元素中,每个元素都有其唯一的名称。我的文件是一个翻译文件,所以它只包含字符串-元素和复数等。
下面是我第一次尝试添加注释时使用的片段:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="debug">You wish you could use that!</string>
<string name="author">foobar</string> <!-- Insert author name here -->
<string name="error">Wutfuq</string> <!-- New! -->
</resources>在这里,我的php文件试图向元素添加一个注释,其名称为"debug":
<?php
error_reporting(E_ALL);
include "SimpleDOM.php";
$xml = simpledom_load_file("strings.xml");
$xml->xpath("//string[@name='debug']")->insertComment(' This is a test comment ', 'after');
$xml -> asXML('test.xml');
echo "This line should be shown, otherwise there might be some error";
?>所以我的问题是这行不通。整个页面都是白色的,没有错误,我的test.xml也不会被创建,所以这个错误似乎出现在我使用xpath时试图添加注释的行。
我很感激你的帮助,请不要试图说服我使用DOM。
发布于 2013-11-04 08:57:56
根据PHP文档,SimpleXMLElement::xpath()返回一个数组。您不能用insertComment()链接它。
编辑:您可以将isertComment与SimpleDom一起使用,但只能在值上使用:
$result = $xml->xpath("//string[@name='debug']")
$result[0]->insertComment(' This is a test comment ', 'after'); // after, append or before
$xml->asXML('test.xml');https://stackoverflow.com/questions/19763799
复制相似问题