Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self-closing tags using createElement

Tags:

dom

php

xml

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/>
like image 963
Lukáš Jelič Avatar asked Sep 24 '11 14:09

Lukáš Jelič


1 Answers

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>
like image 79
Gordon Avatar answered Oct 01 '22 04:10

Gordon