note 124390 added to domdocument.loadxml
| From: | AdrianWiik@php.net | Date: | Wed, 13 Nov 2019 09:02:30 +0000 |
| Subject: | note 124390 added to domdocument.loadxml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-213049@lists.php.net to get a copy of this message | ||
A call to loadXML() will overwrite the XML declaration previously created in the constructor of
DOMDocument. This can cause encoding problems if there is no XML declaration in the loaded XML and
you don't have control over the source (e.g. if the XML is coming from a webservice). To fix
this, set encoding AFTER loading the XML using the 'encoding' class property of
DOMDocument. Example:
Bad situation:
test.xml:
<test>
<hello>hi</hello>
<field>ø</field>
</test>
test.php:
$xmlDoc = new DOMDocument("1.0", "utf-8"); // Parameters here are overwritten
anyway when using loadXML(), and are not really relevant
$testXML = file_get_contents("test.xml");
$xmlDoc->loadXML($testXML);
// Print the contents to a file or in a log function to get the output, using $xmlDoc->saveXML()
Output:
<?xml version="1.0"?>
<test>
<hello>hi</hello>
<field>ø</field>
</test>
Good situation:
test.xml:
<test>
<hello>hi</hello>
<field>ø</field>
</test>
test.php:
$xmlDoc = new DOMDocument("1.0", "utf-8"); // Parameters here are overwritten
anyway when using loadXML(), and are not really relevant
$testXML = file_get_contents("test.xml");
$xmlDoc->loadXML($testXML);
$xmlDoc->encoding = "utf-8";
// Print the contents to a file or in a log function to get the output, using $xmlDoc->saveXML()
Output:
<?xml version="1.0" encoding="utf-8"?>
<test>
<hello>hi</hello>
<field>ø</field>
</test>
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.168 (proxied: 62.50.172.211)
----
Manual Page -- http://php.net/manual/en/domdocument.loadxml.php
Edit -- https://master.php.net/note/edit/124390
Del: integrated -- https://master.php.net/note/delete/124390/integrated
Del: useless -- https://master.php.net/note/delete/124390/useless
Del: bad code -- https://master.php.net/note/delete/124390/bad+code
Del: spam -- https://master.php.net/note/delete/124390/spam
Del: non-english -- https://master.php.net/note/delete/124390/non-english
Del: in docs -- https://master.php.net/note/delete/124390/in+docs
Del: other reasons-- https://master.php.net/note/delete/124390
Reject -- https://master.php.net/note/reject/124390
Search -- https://master.php.net/manage/user-notes.php