Click to See Complete Forum and Search --> : Create XML using DOM functions


Lubox
03-11-2005, 05:01 PM
Hi

I have created a function that creates an XML document using the DOM functions. This was the easiest way I have found to create XML documents. However, I ran into a problem with textnodes and characters from the Norwegian alphabet( Æ Ø Å <- which you probably dont see).

My code:


$xmldoc = new DOMDocument('1.0','iso-8859-1');
$root = $xmldoc->createElement('MYROOT');
$root = $xmldoc->appendChild($root);

$NewChild = $xmldoc->createElement("MYCHILD");
$NewChild = $root->appendChild($NewChild);
$NewChild->appendChild($xmldoc->createTextNode("BØ"));

return $xmldoc ->saveXML();



When I run this, I get:

Warning: output conversion failed due to conv error in my_xml.php on line 116

Warning: Bytes: 0xD8 0x20 0x52 0x75 in my_xml.php on line 116

And the XML-document is cut short there, meaning that after the B in my textnode the XML just stops and will cause an error if I display it in the browser (since no tags has been properply ended).

Any suggestions on how to solve this? I've tried to use no encoding in the "new DOMDocument", and that will help finish the XML doc, but the Norwegian character looks like some chinese sign. I've tried with utf-8 there, but my browser will complain about "an invalid character was found in text content".

Thanks
Lubox

ShrineDesigns
03-11-2005, 08:59 PM
use htmlspecialchars() (http://www.php.net/manual/en/function.htmlspecialchars.php)

Lubox
03-12-2005, 05:26 AM
By using htmlentities instead, the XML doc was created, but the resulting thingy ( &amp;Oslash; ) did not look like an Ø in any cases and cannot be used.
htmlspecialchars does not affect these "special characters" as far as I can see.

I thought ISO-8859-1 could be used to handle norwegian characters, at least that is what I'm using in other tools....

Any other suggestions? Maybe I should create the XML in a different way...maybe as a text string :)

Thanks
Lubox

ShrineDesigns
03-12-2005, 02:07 PM
try placing your text inside <![CDATA[some text...]]>

Lubox
03-12-2005, 05:13 PM
I ended up using the Pear XML_Serializer instead, and that did the trick....

Thanks
Lubox