www.webdeveloper.com
Results 1 to 2 of 2

Thread: Add and edit XML with PHP

  1. #1
    Join Date
    Mar 2009
    Posts
    1

    Post Add and edit XML with PHP

    Hi there!

    Im currently working on a script which allows me to enter new posts in a XML-file.

    The XML ("config.xml") is read by a flash application to show videos. The XML-template is as follow, and is entered for each new movie I want to publish:
    Code:
          <item>
             <title>$title</title> // from form ($_POST)
             <description><![CDATA[$description]]></description> // short description of movie
             <url>$URL</url> // URL to the moviefile (FLV)
             <thumb>$thumb_url</thumb> // URL to thumbnail
          </item>
    I've created a short PHP-script which allows me to enter this details through a form. On the top and the bottom of the file there's a tag <movie> and </movie>, and using the fgets()-function with a "while", the script is searching for the first <movie>-line and place the new entery right below it.

    The source code can be found here: http://norskwebforum.no/pastebin/10961 (text in norwegian, but I think you'll understand the point).

    Now, I really want to add the functionality to edit and delete posts, but I don't know how to do this. Maybe I can generate the XML with a database, but I started with PHP two weeks ago, so I'm still pretty n00b at this. I've read about SimpleXML in PHP5 as a easier way of doing this, but at this point this is a little too complicated to understand...

    Summary: The point is to add, edit and delete items from the XML-file within a graphical interface.

    So any help would've been much appriciated!
    Last edited by Marjoth; 03-17-2009 at 11:48 AM.

  2. #2
    Join Date
    Feb 2009
    Posts
    102
    I would think you could use the same sort of while loop to get each of the items.

    PHP Code:
    $i 1;
    $intag false
    while(whatever...) {
       if(
    $line == "<item>") {
          
    $intag true;
          
    $item_array[$i] = '';
          continue;
       }
       if(
    $line == "</item>") {
          
    $intag false;
          
    $i++;
          continue;
       }
       if(
    $intag) {
          
    $item_array[$i] .= $line "\n";
       }
    }

    return 
    $item_array
    That would give you an array containing all the items, which is pulled directly from the file. Then to delete or edit them, you would use a function that finds the nth instance of <item>...</item> where n is the key from the array.

    It's not the cleanest way of doing it, but at least it doesn't require the information to be stored in the file AND a database. All the work is done right from the file.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles