How to Prevent Self Closing Tag in PHP Simplexml

How to prevent self closing tag in php simplexml

LIBXML_NOEMPTYTAG does not work with simplexml, per the spec:

This option is currently just available in the DOMDocument::save and DOMDocument::saveXML functions.

To achieve what you're after, you need to convert the simplexml object to a DOMDocument object:

$xml = new SimpleXMLElement('<xml/>');
$child1 = $xml->addChild('child1');
$child1->addChild('child2', "value");
$child1->addChild('noValue', '');
$dom_sxe = dom_import_simplexml($xml); // Returns a DomElement object

$dom_output = new DOMDocument('1.0');
$dom_output->formatOutput = true;
$dom_sxe = $dom_output->importNode($dom_sxe, true);
$dom_sxe = $dom_output->appendChild($dom_sxe);

echo $dom_output->saveXML($dom_output, LIBXML_NOEMPTYTAG);

which returns:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
<child1>
<child2>value</child2>
<noValue></noValue>
</child1>
</xml>

Something worth pointing out... the likely reason that the NOEMPTYTAG option is available for DOMDocument and not simplexml is that empty elements are not considered valid XML, while the DOM specification allows for them. You are banging your head against the wall to get invalid XML, which may suggest that the valid self-closing empty element would work just as well.

Turn OFF self-closing tags in SimpleXML for PHP?

From the documentation at SimpleXMLElement->__construct and LibXML Predefined Constants, I think this should work:

<?php
$sxe = new SimpleXMLElement($someData, LIBXML_NOEMPTYTAG);

// some processing here

$out = $sxe->asXML();
?>

Try that and see if it works. Otherwise, I'm afraid, it's preg_replace-land.

SimpleXML: do not expand self-closing tags

Change the way you load the XML to

$xml = simplexml_load_file($path, null, LIBXML_NOEMPTYTAG);

This gives...

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<a>hello</a>
<b attr="1"/>
</root>

Getting simplexml to understand input that have no self closing tags?

There is a trick for this: the DOM extension can parse HTML, including unclosed tags like you have here; and SimpleXML can "import" a DOM object (without actually reparsing anything, because they use the same memory structure underneath).

It ought to be as simple as:

$dom = new DOMDocument;
$dom->loadHTML($html);
$sx = simplexml_import_dom($dom);

how to avoid shortcut closed xml tags if value is empty in simplexml addChild?

Self-closing tags are a normal part of XML, so normally, you should just not worry about them. However, if you really want to avoid them and use the full <mytag></mytag> even when empty, SimpleXML doesn't have that option but DOM does, and you can convert to DOM when you output.

For example, if you're using

$xml->asXML()

Change it to

dom_import_simplexml($xml)->ownerDocument->saveXML(null, LIBXML_NOEMPTYTAG);

How to write XML self closing tag using DOMDocument

Providing the empty string second argument to createElement() adds an empty textnode to the element node. The element is not empty an can not be optimized. Without the argument DOM optimizes the XML.

$dom = new DOMDocument();
$dom->appendChild($dom->createElement('root'));
echo $dom->saveXml();

Output:

<?xml version="1.0"?>
<root/>

Here is an option for saveXml() to avoid the optimization.

$dom = new DOMDocument();
$dom->appendChild($dom->createElement('root'));
echo $dom->saveXml(NULL, LIBXML_NOEMPTYTAG);

Output:

<?xml version="1.0"?>
<root></root>


Related Topics



Leave a reply



Submit