I need to add a self-closing tag to XML file with DOM in PHP, but I don't know how, because standardly, this tag looks like this:
<tag></tag>
But it should look like this:
<tag/>
DOM will do that automatically for you
$dom = new DOMDocument;
$dom->appendChild($dom->createElement('foo'));
echo $dom->saveXml();
will give by default
<?xml version="1.0"?>
<foo/>
unless you do
$dom = new DOMDocument;
$dom->appendChild($dom->createElement('foo'));
echo $dom->saveXml($dom, LIBXML_NOEMPTYTAG);
which would then give
<?xml version="1.0" encoding="UTF-8"?>
<foo></foo>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With