Why is giving error in this PHP to write an XML?

Asked

Viewed 91 times

3

Trying to make a simple XML that takes the data registered in a PHP page but is giving error, someone can tell me what it is and how it can work?

Error that appears when pressing the button:

Fatal error: Uncaught Exception 'Domexception' with message 'Hierarchy Request Error' in C: xampp... cadastro.php:17 Stack trace: #0 C: xampp... cadastro.php(17): Domnode->appendchild(Object(Domelement)) #1 {main} thrown in C: xampp... cadastro.php on line 17

Full page code:

<?php 

if (isset($_POST['insert'])){

$xml= new DOMDocument("1.0", "UTF-8");
$xml->load('studentdb.xml');

$nome = $_POST['nome'];
$endereco = $_POST['endereco'];

$rootTag = $xml->getElementsByTagName("roo")->item(0);

$infoTag = $xml->createElement("info");
    $nomeTag = $xml->createElement("nome", $nome);
    $enderecoTag = $xml->createElement("endereco", $endereco);

    $nomeTag->appendChild($nomeTag);
    $enderecoTag->appendChild($enderecoTag);

$rootTag->appendChild($infoTag);
$xml->save('studentdb.xml');

}
?>

<html>
<body>
<form method="post" action="cadastro.php">
Informacoes <br>
Nome <input type="text" name="nome"> <br>
Endereco <input type="text" name="endereco"> <br>
<input type="submit" name="insert" value="add">
</form>
</body>
</html>
  • 2

    Related epic response (in Soen): http://stackoverflow.com/a/16344986/4734177

  • Adds the file in question.

  • Take a look here: http://php.net/manualen/domdocument.save.php#67952

1 answer

0

This error is because you are trying to move a Node inside yourself.

Here you go an explanation of a question like yours.

I hope I’ve helped.

UPDATING:

Take a look at line 17 and 18 of your code. I believe that’s the problem.

$nomeTag->appendChild($nomeTag);
$enderecoTag->appendChild($enderecoTag);

  • Bludgeoned, I was frightened by the post photo kkkkk.

  • kkkkkkkkkkkk tb I found a lot loco, but there’s no way I can’t help looking at her kkkkkkkkkkkkkkkk

  • until I understood what is happening, I just didn’t understand very well how to fix :/

  • I edited the answer, I believe it’s a problem on line 17 E18

Browser other questions tagged

You are not signed in. Login or sign up in order to post.