PDA

View Full Version : Creating and Editing XML




noelister
Sep 28, 2006, 09:50 AM
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
Sep 28, 2006, 11:41 AM
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:

$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:

$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 (http://www.php.net/manual/en/ref.domxml.php) instead.

noelister
Sep 28, 2006, 02:52 PM
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.

Kunimodi
Sep 28, 2006, 04:17 PM
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.

Certainly. I'd be happy to help.