I would like the ability to edit a single XML file using a PHP form. I have scoured the net and haven't found any clear tutorial on how to do this. I'm fairly new to PHP and XML so I'm sure this is amature stuff!

Here's what my xml file looks like:

<songs>
<song title="Stormy Nights" artist="Jeff Jones" path="/streaming/StormyNights.mp3"/>
<song title="Woo Hoo" artist="The Superstars" path="/streaming/WooHoo.mp3"/>
<song title="Sunny Days" artist="The Big Band" path="/streaming/SunnyDays.mp3"/>
</songs>

So the PHP form fields would include:
Song Title
Artist
Path (which preferably would be a file upload field which automatically insert the correct mp3 file name and path into the xml file)

I tried for hours to get this to work but I just can't figure it out! Any help greatly appreciated!
Thanks!

    Do you have access to the [man]DOMXML[/man] functions?

      I do not know 😮

      Is there a way (or script) for me to easily find out?!?
      Thanks!

        9 months later

        Does anyone know a relatively easy way to solve this problem?

          Hello,

          Might as well make my first post usefull 🙂

          why not use fopen, fwrite and fclose very simple to use. Looks like you might be making a cms for a flash mp3 player?

          HEre is how I have done it.

          Here is a short tutorial and then the code.

          Make your xml file.

          Lets call it. songs.xml

          Make a new file, lets call it fopen.php this will be the code to open the xml file and then write the data from the form.

          I create a variable liveconfig, then use that to store the name if the file IA m editng, this make it easy to use the same code to edit multiple files.

          Code for fopen.php

          <?php

          $liveconfig = "shows.xml";
          
          if ($op == "update" ) {
              $fp = fopen($liveconfig, "w") or die("error writing to config file, please contact the admin\n");
          	$data = stripslashes($data); 
              fwrite ($fp, "$data");
          
          fclose($fp);
          
          if (file_exists($liveconfig)) {
              $updated = 1;
          	echo ("data saved, <a href=\"javascript:history.go(-1)\">go back</a>");
          }
          
          }

          ?>

          now we need to make a page that has the form field so we can edit the xml, and then also post the data to fopen.php

          I made a new file called form.php

          I also use the variable liveconfig in order to use the same code to edit multiple xml files.

          <html>
          <head>
          <title>Edit XML</title>
          <meta http-equiv="Content-Type" content="text/html;">

          </head>
          <body>
          <form name="form1" method="post" action="fopen.php?op=update">
          <textarea name="data" style="width:640px; height:300px">
          <? include ($liveconfig); ?>
          </textarea>
          <input type="submit" name="Submit" value="Submit">

          </form>
          </body>
          </html>

          then upload to server and chmod shows.xml so it writable by the webserver.

          Then pull up form.php?liveconfig=shows.xml

          edit the xml and hit save, you got a simple cms.

          I prefer using my sql, but this is great for simple flash or html websites.

          I hope this helps.

            Just another option. . .

            Here's "books.xml":

            <?xml version="1.0" encoding="ISO-8859-1"?>
            <?xml-stylesheet type="text/xsl" href="book.xsl"?>
            <list>
            <book>
                <name>The Canterbury Tales  </name>
                <fname>Geoffrey </fname>
                <lname>Chaucer </lname>
            </book>
            
            <book>
                <name>The Last of the Mohicans  </name>
                <fname>James F. </fname>
                <lname>Cooper</lname>
            </book>
            
            <book>
                <name>The Great Gatsby  </name>
                <fname>F. Scott </fname>
                <lname>Fitzgerald</lname>
            </book>
            
            <book>
                <name>The Fellowship of the Ring  </name>
                <fname>J. R. R. </fname>
                <lname>Tolkien</lname>
            </book>
            
            <book>
            <name>The Two Towers  </name>
            <fname>J. R. R. </fname>
            <lname>Tolkien</lname>
            </book>
            
            <book>
            <name>Hunchback of Notre Dame</name>
            <fname>Victor </fname>
            <lname>Hugo</lname>
            </book>
            
            <book>
            <name>Lirael</name>
            <fname>Garth </fname>
            <lname>Nix </lname>
            </book>
            </list>
            

            Here's the PHP form/code:

            <html>
            <head>
            <title>
            Books
            </title>
            </head>
            <body>
            <form method="post" action="book.php">
            <p>Title:<input type="text" name="title" /><br />
            Author's First Name:<input type="text" name="fname" /><br />
            Author's Last Name:<input type="text" name="lname" /><br />
            <input type="submit" value="Add Book" /></p>
            </form>
            
            <?php
            $title = $_POST['title'];
            $fname = $_POST['fname'];
            $lname = $_POST['lname'];
            
            $books = file('booklist.xml');
            
            $numOfLines = count($books) - 1;
            
            $books[$numOfLines]="";
            
            $file = fopen("booklist.xml", 'w');
            
            foreach( $books AS $val ) {
                fwrite($file, $val);
             }
            $content = "<book>
            <name>{$title}</name>
            <fname>{$fname}</fname>
            <lname>{$lname}</lname>
            </book>\n";
            fwrite($file, $content);
            fwrite($file, "</list>");
            
            fclose($file);
            
            $file = fopen("printbooks.xml", 'w');
            foreach( $books AS $val ) {
                            fwrite($file, $val);
            }
             $content = "<book>
            <name>{$title}  </name>
            <fname>{$fname} </fname>
            <lname>{$lname}</lname>
            </book>\n";
            fwrite($file, $content);
            fwrite($file, "</list>");
            
            fclose($file);
            ?>
            </html>
            
              Write a Reply...