Creating and Editing XML

Discussion in 'Mac Programming' started by noelister, Sep 28, 2006.

  1. macrumors 6502

    Joined:
    Jan 15, 2005
    #1
    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,
     
  2. macrumors member

    Joined:
    Sep 8, 2006
    Location:
    Ashland, OR, USA
    #2
    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.
     
  3. thread starter macrumors 6502

    Joined:
    Jan 15, 2005
    #3
    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.
     
  4. macrumors member

    Joined:
    Sep 8, 2006
    Location:
    Ashland, OR, USA
    #4
    Certainly. I'd be happy to help.
     

Share This Page