I have an XML string that sometimes has empty nodes. When parsing this with simplexml_load_string, the parser interprets any empty nodes (example <node></node> ) as an empty SimpleXMLElement. I would prefer that they go through an empty line or just be completely omitted.
I tried using LIBXML_NOBLANKS as shown below, but it does not seem to be affected. Here is some code that demonstrates the situation. node "p2" is empty:
$xml = "<xml><p1>1</p1><p2></p2><p3>3</p3></xml>"; $obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOBLANKS); header("Content-type: text/plain"); echo "STRING\n-----\n" . $xml; echo "\n\nOBJ\n---\n" . print_r($obj,1); echo "\n\nJSON\n----\n" . json_encode($obj);
source share