note 67952 added to function.dom-domdocument-save

From: Date: Fri, 07 Jul 2006 15:00:53 +0000
Subject: note 67952 added to function.dom-domdocument-save
Groups: php.notes 
Request: Send a blank email to php-notes+get-114550@lists.php.net to get a copy of this message
The XML parser converts the text of an XML document into UTF-8, even if you have set the character encoding of the XML, for example as a second parameter of the DOMDocument constructor. After parsing the XML with the load() command all its texts have been converted to UTF-8. In case you append text nodes with special characters (e. g. Umlaut) to your XML document you should therefore use utf8_encode() with your text to convert it into UTF-8 before you append the text to the document. Otherwise you will get an error message like "output conversion failed due to conv error" at the save() command. See example below: <?php // Text to insert into XML below $txt = "a text with special characters like 'ä', 'ß', 'Ü' etc."; // Create Instance of DOMDocument $dom = new DOMDocument; // Load XML file // Was created before with DOMDocument('1.0', 'iso-8859-1') $dom = $dom->load("file.xml"); // Find the parent node $parent = $dom->documentElement; // Create Instance of DomXPath $xpath = new DomXPath($dom); // new node will be inserted before this node $next = $xpath->query("//parentnode/childnode"); // Create the new element $new_elem = $dom->createElement('new_elem'); // Insert the new element $parent->insertBefore($new_elem, $next->item(0)); // DOMXML = utf-8! (will be converted to iso-8859-1 only at 'save()') // prevents error message "output conversion failed due to conv error" at 'save()' $txt = utf8_encode($txt); // Create new text node with utf-8 encoded string $nodetext = $dom->createTextNode("$txt"); // Append text node to new element $nodetext = $new_elem->appendChild($nodetext); // save $dom->save("file.xml"); ?> Hope this helps someone. siegparr ---- Server IP: 66.163.161.117 Probable Submitter: 212.114.208.70 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.dom-domdocument-save.php Edit -- http://master.php.net/note/edit/67952 Del: integrated -- http://master.php.net/note/delete/67952/integrated Del: useless -- http://master.php.net/note/delete/67952/useless Del: bad code -- http://master.php.net/note/delete/67952/bad+code Del: spam -- http://master.php.net/note/delete/67952/spam Del: non-english -- http://master.php.net/note/delete/67952/non-english Del: in docs -- http://master.php.net/note/delete/67952/in+docs Del: other reasons-- http://master.php.net/note/delete/67952 Reject -- http://master.php.net/note/reject/67952 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#114550) next »