Mac Creating and Editing XML

noelister

macrumors 6502
Original poster
Jan 15, 2005
275
0
Hello,

I have a little project started in flash that will organize and display my movies. I am using ActionScript to parse an XML file and grab the information like Movie title, Movie Date, and Description. However, I would like to create an admin page that could create a new XML file or edit an existing file through the web. My web server has support for PHP. Can anyone think of a solution?

Thanks,
 

Kunimodi

macrumors member
Sep 8, 2006
65
0
Ashland, OR, USA
noelister said:
Hello,

I have a little project started in flash that will organize and display my movies. I am using ActionScript to parse an XML file and grab the information like Movie title, Movie Date, and Description. However, I would like to create an admin page that could create a new XML file or edit an existing file through the web. My web server has support for PHP. Can anyone think of a solution?

Thanks,
Hi, Noelister. PHP has good support for working with XML doocuments. To create a new XML file you could do it like this:

Code:
$movie_doc = new DOMDocument();
$root_elem = $movie_doc->createElement('movies');
$movie_elem = $movie_doc->createElement('movie');
$movie_elem->setAttribute('title', 'The Island');
$movie_elem->setAttribute('date', '2005');
$desc_elem = $movie_doc->createElement('description'); // or createElement('description', 'Action sci-fi movie.')
$desc_text = $movie_doc->createTextNode('Action sci-fi movie.');
$desc_elem->appendChild($desc_text);
$movie_elem->appendChild($desc_elem);
$root_elem->appendChild($movie_elem);
$movie_doc->appendChild($root_elem);
$movie_doc->save('movie.xml');
$raw_xml_string = $movie_doc->saveXML();
To work with an existing XML file, load it at the beginning:

Code:
$movie_doc = new DOMDocument();
$movie_doc->load('movie.xml');
foreach ($movie_doc->documentElement->childNodes as $movie_node) {
    if ($movie_node->nodeType == XML_ELEMENT_NODE && $movie_node->nodeName == 'movie') {
        $title = $movie_node->getAttribute('title');
    }
}
More information on DOM based XML handling is available at http://www.php.net/manual/en/ref.dom.php. Note that if you have an older verison of PHP, you may have to use the deprecated DOM library instead.
 

noelister

macrumors 6502
Original poster
Jan 15, 2005
275
0
Okay, I will work on getting something together but I may be sending you a PM if I run into any trouble, if that is okay.