Question

I'm trying to add a comment in a xml file to a specific element, every element has it's unique names. My file is a translation file so it only contains string-elements and plurals etc.

Here's the snippet I use for my first attempt to add a comment:

<?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>

And here my php file where I try to add a comment to the element with the name "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";
?>

So my problem is that this won't work. The whole page is white, there is no error and my test.xml won't be created, so the mistake seems to be at the line where I'm trying to add the comment while using xpath.

I'd be grateful for help and please don't try to convince me to use DOM.

Was it helpful?

Solution

According to the PHP doc, SimpleXMLElement::xpath() returns an array. You can't chain it with insertComment().

EDIT : You can use isertComment with SimpleDom but only on the value :

$result = $xml->xpath("//string[@name='debug']")
$result[0]->insertComment(' This is a test comment ', 'after'); // after, append or before
$xml->asXML('test.xml');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top