0

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

3
  • Which one do you have in your actual code, $xml->root->... or $xml->$root->... ? Commented Oct 2, 2016 at 4:39
  • If I use $xml->root->appendChild() then the error is DOMDocument::$root If I use $xml->$root->appendChild() then the error is DOMDocument::root But I am using "root" as I understand that to be the more correct way of doing it. The OP is my exact code. Commented Oct 2, 2016 at 4:41
  • Ok, but the error message doesn't correspond to the code, hence it is confusing. The error message that I got with $xml->root : Fatal error: Call to a member function appendChild() on null Commented Oct 2, 2016 at 4:54

2 Answers 2

1

$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.";
}
Sign up to request clarification or add additional context in comments.

1 Comment

When I started the code I was trying to use the SimpleXMLElement and couldn't get it to read the file. I ended up with the DOMDocument, not realizing it was a different structure. Though this syntax is longer, I'm more comfortable with it because it is like JavaScript and XML. Thanks for your help.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.