Re: How do I write to an XML file
| From: | memoimyself at yahoo dot com dot br | Date: | Mon, 12 Jan 2004 13:45:58 +0000 |
| Subject: | Re: How do I write to an XML file | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-174320@lists.php.net to get a copy of this message | ||
Hello Tim,
On 12 Jan 2004 at 17:50, Tim Burgan wrote:
> I'm wondering if anyone can help me or point me in the right direction
> regarding how I can write to an XML file.
Writing to an XML file works the same way as writing to any other file: you need to open
the file with fopen(), append to or overwrite the file contents with fwrite(), and close the
file with fclose().
However, In order to do something useful with the XML in your files, you'll also want to
read the file right after opening it (i.e. before writing anything to it), parse the XML, insert
new elements/attributes/text nodes into the XML, and then write that to the file.
The XML parsing and manipulating can be accomplished with the DOM XML functions
(see chapter XXIV of the function reference section of the PHP manual) or the XML
Parser (Expat) functions (see chapter CXI of the function reference section of the PHP
manual). Both sets of functions are PHP extensions, which means they are not enabled
by default; on *nix systems, you need to compile these extensions when installing PHP,
and on Windows systems you need to enable the extensions in the php.ini file.
Alternatively, if you cannot or don't want to install/reinstall PHP on the web server
you're
going to use, you can use PHP classes that emulate the DOM XML extension. The best
ones I know of are miniXML (http://minixml.psychogenic.com) and Domit!
(http://www.engageinteractive.com/domit/); both are free and come with fairly good
documentation that will explain how to use them. There probably are a couple of
classes out there to do Expat-style (chunk by chunk) parsing, but in your case you'll
probably want to stick with DOM-style parsing.
Good luck,
Erik