Page 1 of 1

Writing XML with PHP

Posted: Wed Mar 07, 2007 12:30 pm
by chuckgrenade
I as trying to write this XML using this code. The XML data comes from FLEX and is sent to the PHP code. The code writes to the file fine, but around the entire xml that was sent from flex, there are additional < /> around all of the data which caused my xml document to be in error. I'm pretty sure it's the add-root function in there but I don't know alot about PHP and have no idea on what to change it to.

Code: Select all

<?
$doc = new_xmldoc('1.0');
$XMLData = stripSlashes($XMLData);
$root = $doc->add_root($XMLData);
$fp = @fopen($path,'w');
if(!$fp) {
    die('Error cannot create XML file');
}
fwrite($fp,$doc->dumpmem());
fclose($fp);

?>
Thanks in advance!!

Posted: Wed Mar 07, 2007 1:16 pm
by RobertGonzalez
Can you show the addroot function code. And try to stay away from using fifty thousand exclamation points. That has a tendency to turn people away from posts around here.

Posted: Wed Mar 07, 2007 1:32 pm
by chuckgrenade
Sorry, add_root is in PHP as DomDocument->add_root
The code I used in the PHP is above. Basically, it works, except that it put everything between < and />.
So after the xmlData is passes to the PHP it turn out as

<<stuff>
<moreStuff attribute="PHP" />
</stuff>/>

Posted: Wed Mar 07, 2007 1:48 pm
by volka
add_root is marked as deprecated.
The parameter you pass is the element's name, like in add_root('foobar') => <foobar></foobar>
You want domxml_open_mem or maybe domxml_open_file

Posted: Wed Mar 07, 2007 1:57 pm
by chuckgrenade
AH - Thank you - it seems to have worked using domxml_open_mem :)