note 75945 added to function.simplexml-element-asXML
| From: | FredTrotter at osu1 dot php dot net | Date: | Fri, 22 Jun 2007 22:27:53 +0000 |
| Subject: | note 75945 added to function.simplexml-element-asXML | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127894@lists.php.net to get a copy of this message | ||
When trying to use the DOMDocument method for pretty printing the output of asXML be careful how you
format the starting XML string. It cannot have spaces or other hidden chars "\n". I also
had trouble with using different types of quotes for the starting XML, but I am unable to replicate
that. So if you cannot get this to work then you might try swapping the single and double quotes.
<?php
$no_spaces = '<?xml version="1.0"
encoding="UTF-8"?><Example></Example>';
$one_space = '<?xml version="1.0" encoding="UTF-8"?><Example>
</Example>';
echo printy($no_spaces);
/* Outputs
<?xml version="1.0"?>
<Example>
<poop from="Myself">
<smell>stinky</smell>
</poop>
</Example>
*/
echo printy($one_space);
/* Outputs... note the space...
<?xml version="1.0"?>
<Example> <poop
from="Myself"><smell>stinky</smell></poop></Example>
*/
function printy($xmlstr){// a combination from the comments below...
$xml = new SimpleXMLElement($xmlstr);
$poop = $xml->addChild("poop");
$poop->addAttribute("from", "Myself");
$poop->addChild("smell", "stinky");
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$domnode = dom_import_simplexml($xml);
$domnode = $doc->importNode($domnode, true);
$domnode = $doc->appendChild($domnode);
return $doc->saveXML();
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 97.99.130.153
----
Manual Page -- http://www.php.net/manual/en/function.simplexml-element-asXML.php
Edit -- https://master.php.net/note/edit/75945
Del: integrated -- https://master.php.net/note/delete/75945/integrated
Del: useless -- https://master.php.net/note/delete/75945/useless
Del: bad code -- https://master.php.net/note/delete/75945/bad+code
Del: spam -- https://master.php.net/note/delete/75945/spam
Del: non-english -- https://master.php.net/note/delete/75945/non-english
Del: in docs -- https://master.php.net/note/delete/75945/in+docs
Del: other reasons-- https://master.php.net/note/delete/75945
Reject -- https://master.php.net/note/reject/75945
Search -- https://master.php.net/manage/user-notes.php