user1544522
user1544522

Reputation: 43

PHP appendChild to XML root

I know this should be simple, but I'm new to PHP and just do not understand the documentation I've come across. I need a simple explanation.

I have an XML document that I would like to add nodes to. I can add nodes to the document, they only appear outside of the root node and cause errors to happen on subsequent attempts.

Here is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>

and here is my PHP:

$playerID = "id_" . $_POST['player'];

//load xml file to edit
$xml = new DOMDocument();
$xml->load('../../saves/playerPositions.xml') or die("Error: Cannot load file.");

//check if the player is already in the database
if(isset($xlm->$playerID)){
    echo "Player ID was found.";
}
else{
    echo "Player ID was not found.";

    //so we want to create a new node with this player information
    $playerElement = $xml->createElement($playerID, "");
    $playerName = $xml->createElement("name", "John Doe");

    //add the name to the playerElement
    $playerElement->appendChild($playerName);

    //add the playerElement to the document
    //THIS IS WHERE THE ERROR OCCURS
    $xml->root->appendChild($playerElement);

    //save and close the document
    $xml->formatOutput = true;
    $xml->save("../../saves/playerPositions.xml");
}

echo "done";

If I just use $xml->appendChild() then I can modify the document, but the new text appears outside of <root></root>.

The exact error is:

Notice: Undefined property: DOMDocument::$root

Upvotes: 0

Views: 3647

Answers (2)

har07
har07

Reputation: 89285

$xml->root isn't the correct way to access root element in this context, since $xml is an instance of DOMDocument (It will work if $xml were SimpleXMLElement instead). You can get root element of a DOMDocument object from documentElement property :

$xml->documentElement->appendChild($playerElement);

eval.in demo 1

Or, more generally, you can get element by name using getElementsByTagName() :

$xml->getElementsByTagName("root")->item(0)->appendChild($playerElement);

eval.in demo 2

Further reading : PHP DOM: How to get child elements by tag name in an elegant manner?


That said, this part of your code is also not correct for the same reason (plus a typo?):

if(isset($xlm->$playerID)){
    echo "Player ID was found.";
}

Replace with getElementsByTagName() :

if($xml->getElementsByTagName($playerID)->length > 0){
    echo "Player ID was found.";
}

Upvotes: 1

espino316
espino316

Reputation: 452

You are trying to handle the dom like a Standar Object, but is not. To look for elements, you need to use the function getElementsByTagName, and handle the dom like a collection of nodes, like this:

$xml = new DOMDocument();
    $xml->loadXML('<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>') or die("Error: Cannot load file.");

    $len = $xml->getElementsByTagName('player')->length;
    if ( $len > 0 ) {

    } else {
      $player = $xml->createElement('player', '');
      $playerName = $xml->createElement('playerName', "Jhon Doe");
      $player->appendChild( $playerName );

      $root = $xml->getElementsByTagName('root');

      if ( $root->length > 0 ) {
        $root[0]->appendChild( $player );
      }

      $xml->formatOutput = true;
      echo $xml->saveXML();
    }

This code produces:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<player><playerName>Jhon Doe</playerName></player></root>

Upvotes: 0

Related Questions