PDA

View Full Version : Writing to XML files with PHP



foodpk
March 6th, 2005, 07:45 AM
XML is really cool, I'm amazed at how easy it is to use and what it can achieve. But now I'm wondering about what the easiest method of adding data into an XML file via PHP is. I'm somewhat proficient with PHP (and MySQL), just not too much. For instance we have an XML file like so.


<news>
<item>
<heading>This is the heading!</heading>
<content>Content! Blah blah blah blah blah blah blah!</content>
</item>
<item>
<heading>This is the heading!</heading>
<content>Content! Blah blah blah blah blah blah blah!</content>
</item>
<item>
<heading>This is the heading!</heading>
<content>Content! Blah blah blah blah blah blah blah!</content>
</item>
</news>

So how would we go about adding item elements (of course containing headings and content) into our XML file via PHP? Are there any good libraries that do that or built in functions?

foodpk
March 6th, 2005, 04:47 PM
*gentle bump, I hate bumping, but it must be done*

icio
March 6th, 2005, 05:43 PM
try posting it into the correct forum:
http://www.kirupa.com/forum/forumdisplay.php?f=11

there is also a tutorial on this somewhere on kirupa.com

but - to get things into the XML file that is loaded, you echo them.

echo ("<xml><item>item1</item><item>item2</item></xml>");

and you would use loops and variables to put in what you want :thumb:

foodpk
March 6th, 2005, 06:01 PM
Hmmm, I'm not sure I follow, you just echo them and they write into the xml file?
Oh, by the way, I didn't even see that forum existed, I apologize. It would be cool if one of the mods could move this thread there so I don't have to make another one.

scotty
March 7th, 2005, 04:50 AM
...... It would be cool if one of the mods could move this thread there so I don't have to make another one.
Done;)

scotty(-:

petefs
March 7th, 2005, 05:33 AM
PHP5 has fantastic XML support. Check out simplexml: http://us3.php.net/manual/en/ref.simplexml.php

What cyberathlete says works as well and is a nice and easy way to do things for simple xml files.

There are a variety of methods of dealing with XML in PHP4, I suggest starting by looking at the expat, domxml, etc... sections of php.net if you really want to get into it.

icio
March 7th, 2005, 05:15 PM
well, no - it doesn't write to the xml file - the php is the xml file.
for example, if we were loading an xml file into flash - we wouldn't load an xml file, we would load the php file.

the echo function writes to the file that is then sent to the user when the download it via http.
for example, if i goto http://www.website.com/phpFile.php and in that file is:

echo("this is being generated by the server");the user would only see, "this is being generated by the server" minus the quotation marks. i don't know how clear i'm making myself. :puzzled:

if you were wanting to load this into flash, you'd load xml.php - which then echoes the information that is required, in the correct order.

:S
good thing to do is check out the tutorial on kirupa.com main page :thumb:

KaiserSouze
March 7th, 2005, 05:52 PM
Or if you really need a proper XML file rather than a PHP page generating XML, you can just write all of the XML in a variable, and then get PHP to generate the actual XML file like this:



$xmlContent = '<xml><content /></xml>';
$filename = 'xmlFile.xml';
$fp = fopen($filename, "w");
$write = fputs($fp, $xmlContent);
if(!fclose($fp)) {
echo "Error creating file";
} else {
echo "File Created";
}

Description of fopen (http://uk.php.net/manual/en/function.fopen.php)
Description of fputs (http://uk.php.net/manual/en/function.fputs.php)
And a description of fclose (http://uk.php.net/manual/en/function.fclose.php)

Hope that helps :afro: