# Prasing XML using PHP

**URL:** <https://forum.kirupa.com/t/prasing-xml-using-php/281757>\
**Category:** programming\
**Created:** [February 12, 2009, 11:19pm UTC](https://forum.kirupa.com/t/prasing-xml-using-php/281757 "2009-02-12T23:19:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzeno](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@dzeno](https://forum.kirupa.com/u/dzeno)\
**Post date:** [February 12, 2009, 11:19pm UTC](https://forum.kirupa.com/t/prasing-xml-using-php/281757/1 "2009-02-12T23:19:24Z")

</div>

Hello, i have founded script on kirupa, here is test online: [http://prva-gimnazija.edu.ba/list/index.php](http://prva-gimnazija.edu.ba/list/index.php) With this script I can ADD/DELETE content in XML file. I think i can use it to make some simple CMS for my flash website because flash all content reads from XML. What i need is form for EDITING existing nodes in XML file,and after editing to save that to my XML. So like that i will have fully XML update system.I’m beginner in PHP scripting so if someone can help me that would be great.

Here is processForm.php in wich is xml praser:

```php
<?
$songs = Array();
function start_element($parser, $name, $attrs){
    global $songs;
    if($name == "song"){
        array_push($songs, $attrs);
    }
}
function end_element ($parser, $name){}
$playlist_string = file_get_contents("playlist.xml");
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse($parser, $playlist_string) or die("Error parsing XML document.");
print "<br />";
if($_POST['action'] == "ins"){
    array_push($songs, Array(
                "title" => $_POST['name'],
                "artist" => $_POST['artist'],
                "path" => $_POST['path']));
    $songs_final = $songs;
}else if($_POST['action'] == "del"){
    $songs_final = Array();
    foreach($songs as $song){
        if($song['title'] != $_POST['name']){
            array_push($songs_final, $song);
        }
    }
}
$write_string = "<songs>";
foreach($songs_final as $song){
    $write_string .= "<song title=\"$song[title]\" artist=\"$song[artist]\" path=\"$song[path]\" />";
}
$write_string .= "</songs>";
$fp = fopen("playlist.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Song inserted or deleted successfully :)</em><br />";
print "<a href=\"index.php\" title=\"return\">Return</a>";
?>

```

Here is index.php :

```php
<body>
<form action="processForm.php" method="post">
<fieldset>
<label for="name">Name:</label> <input type="text" id="name" name="name"/><br />
<label for="artist">Artist:</label><input type="text" id="artist" name="artist" /><br />
<label for="path">Path:</label> <input type="text" id="path" name="path" /> <br />
<select name="action">
<option value="ins">Insert</option>
<option value="del">Delete</option>
</select>
<input type="submit" />
</fieldset>
</form>
<p>Current entries:</p>
<?php
function start_tag($parser, $name, $attrs){
    global $table_string;
    $table_string .= "<tr><td>$attrs[title]</td><td>$attrs[artist]</td><td>$attrs[path]</td></tr>";
}
function end_tag($parser, $name){}
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($parser, "start_tag", "end_tag");
$table_string = "<table>
        <tr><th>Title</th><th>Artist</th><th>Path</th></tr>";
xml_parse($parser, file_get_contents("playlist.xml")) or die("Error parsing XML file");
$table_string .= "</table>";
echo $table_string;

?>
</body>

```

Here is playlist.xml :

```php
<songs><song title="On Mercury" artist="Red Hot Chili Peppers" path="/red-hot-chili-peppers/on-mercury.mp3" /><song title="Universally Speaking" artist="Red Hot Chili Peppers" path=" /red-hot-chili-peppers/universally-speaking.mp3" /></songs>

```

Thanks
