I have trouble to load XML document into DOM preserving empty tags and null-size strings. Here the example:
$doc = new DOMDocument("1.0", "utf-8");
$root = $doc->createElement("root");
$doc->appendChild($root);
$element = $doc->createElement("element");
$root->appendChild($element);
echo $doc->saveXML();
produces following XML:
<?xml version="1.0" encoding="utf-8"?>
<root><element/></root>
Empty element, exactly as expected. Now let's add empty text node into element.
$doc = new DOMDocument("1.0", "utf-8");
$root = $doc->createElement("root");
$doc->appendChild($root);
$element = $doc->createElement("element");
$element->appendChild($doc->createTextNode(""));
$root->appendChild($element);
echo $doc->saveXML();
produces following XML:
<?xml version="1.0" encoding="utf-8"?>
<root><element></element></root>
Non-empty element with null-size string. Good! But when I am trying to do:
$doc = new DOMDocument();
$doc->loadXML($xml);
echo $doc->saveXML($doc);
on these XML documents I always get
<?xml version="1.0" encoding="utf-8"?>
<root><element/></root>
ie null-size string is removed and just empty element is loaded. I believe it happens on loadXML(). Is there any way to convince DOMDocument loadXML() not to convert null-size string into empty element? It would be preferable if DOM would have TextNode with null-size string as element's child.
Solution is needed to be in PHP DOM due to the way what would happen to the loaded data further.