首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xpath添加SimpleDOM注释

使用xpath添加SimpleDOM注释
EN

Stack Overflow用户
提问于 2013-11-04 08:12:47
回答 1查看 105关注 0票数 0

我试图将xml文件中的注释添加到特定的元素中,每个元素都有其唯一的名称。我的文件是一个翻译文件,所以它只包含字符串-元素和复数等。

下面是我第一次尝试添加注释时使用的片段:

代码语言:javascript
复制
<?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":

代码语言:javascript
复制
<?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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-04 08:57:56

根据PHP文档,SimpleXMLElement::xpath()返回一个数组。您不能用insertComment()链接它。

编辑:您可以将isertComment与SimpleDom一起使用,但只能在值上使用:

代码语言:javascript
复制
$result = $xml->xpath("//string[@name='debug']")
$result[0]->insertComment(' This is a test comment ', 'after'); // after, append or before
$xml->asXML('test.xml');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19763799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档