Writing XML with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chuckgrenade
Forum Newbie
Posts: 3
Joined: Wed Mar 07, 2007 12:22 pm

Writing XML with PHP

Post 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!!
Last edited by chuckgrenade on Wed Mar 07, 2007 1:26 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
chuckgrenade
Forum Newbie
Posts: 3
Joined: Wed Mar 07, 2007 12:22 pm

Post 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>/>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
chuckgrenade
Forum Newbie
Posts: 3
Joined: Wed Mar 07, 2007 12:22 pm

Post by chuckgrenade »

AH - Thank you - it seems to have worked using domxml_open_mem :)
Post Reply