0

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.

Leo Pflug
  • 544
  • 3
  • 15

1 Answers1

1

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');
Martial
  • 1,446
  • 15
  • 27
  • Okay. So what would be the correct way to access this specific element(string) with the attribute name="debug"? – Leo Pflug Nov 04 '13 at 09:22
  • Thanks, that did the trick, although I'll be using it like this: current($xml->xpath("//string[@name='debug']"))->insertComment(' This is a test comment ', 'after'); – Leo Pflug Nov 04 '13 at 10:05
  • Also please fix the typos you made. Some people might copy the code and it will return errors because you have a typo at the first line($resul). – Leo Pflug Nov 04 '13 at 10:06