'Close tag in DOMDocument (XML PHP)

i need help!! I want to close a "order" tag, but the returns tag look like this:

<order\>

My php code it looks like this:

                    $root = $doc->createElement('order');
                    $root = $doc->appendChild($root);

                    $data = $doc->createElement('data');
                    $data = $doc->appendChild($data);

                    $codigos = $doc->createElement('orderID'); // etiqueta
                    $codigos = $data->appendChild($codigos); // etiqueta
                    $textart = $doc->createTextNode($orden); // var del texto
                    $textart = $codigos->appendChild($textart); // ingreso al xml

                    $res = $doc->createElement('metodoCompra');
                    $res = $data->appendChild($res);
                    $textres = $doc->createTextNode($metodoCompra);
                    $textres = $res->appendChild($textres);

                    $resMet = $doc->createElement('metodoPago');
                    $resMet = $data->appendChild($resMet);
                    $textres = $doc->createTextNode($metodoPago);
                    $textres = $resMet->appendChild($textres);

                    $resFec = $doc->createElement('fecha');
                    $resFec = $data->appendChild($resFec);
                    $textres = $doc->createTextNode($fecha);
                    $textres = $resFec->appendChild($textres);

                    $res = $doc->createElement('emailComprador');
                    $res = $data->appendChild($res);
                    $textres = $doc->createTextNode($email);
                    $textres = $res->appendChild($textres);

                    $res = $doc->createElement('totalCompra');
                    $res = $data->appendChild($res);
                    $textres = $doc->createTextNode($total);
                    $textres = $res->appendChild($textres);

I would like the return to be:

<order>
<data>
<orderID>108</orderID>
<metodoCompra>Envio a coordinar</metodoCompra>
<metodoPago>Giro Bancario</metodoPago>
<fecha>09-08-2016</fecha>
<emailComprador>[email protected]</emailComprador>
<totalCompra>6565.2000</totalCompra>
</data>
</order>

But it looks like this:

<order/>
<data>
<orderID>108</orderID>
<metodoCompra>Envio a coordinar</metodoCompra>
<metodoPago>Giro Bancario</metodoPago>
<fecha>09-08-2016</fecha>
<emailComprador>[email protected]</emailComprador>
<totalCompra>6565.2000</totalCompra>
</data>

There is something I am doing wrong and I do not know what it is Any ideas?



Solution 1:[1]

You did not append the child nodes to the right parent node. In DOM you use methods from the document (DOMDocument::create*) to create a node and methods of the parent node (\DOMNode::appendChild(), \DOMNode::insertBefore()) to attach it.

So first create a document object and add the order element.

$document = new \DOMDocument();
$order = $document->appendChild($document->createElement('order'));

\DOMNode::appendChild() returns the appended node, so you can nest the create call. Next you create the data element and append it to the order node.

$data = $order->appendChild($document->createElement('data'));

You appended the this node to the document. This results in an empty order node. Empty XML nodes can be written in the short syntax <tag/>.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ThW