1

I'm trying to make the PHP save the XML file while making it organized by using new lines. I'm trying to use $dom->formatOutput = true; but I keep getting this error:

Warning: DOMDocument::loadXML() [domdocument.loadxml]: Empty string supplied as input in /public_html/rejectlist/processForm.php on line 48

Here's my PHP file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Adding Songs...</title>
<style type="text/css">
em {
    text-align:center;
}
</style>
</head>
<body>
<p>
<?
$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>";
$dom = new DomDocument();
$dom->loadXML($songs);
$dom->formatOutput = true;
$formatedXML = $dom->saveXML();
$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>";
?>
</p>
</body>
</html>

Thanks for the help

Sosa
  • 814
  • 1
  • 9
  • 20
  • Is there any reason why you use the filesytem-methods like file_get_contents, fopen/fwrite? The DOM has own methods like `DOMDocument->load()`(load a document from a file) and `DOMDocument->save()`(save a document into a file)? – Dr.Molle Nov 12 '12 at 04:09
  • To be honest I have no clue what i'm doing. I'm working off [this file here](http://www.kirupa.com/forum/showthread.php?51044-How-can-I-edit-an-xml-file-through-a-php-form&p=466908&viewfull=1#post466908), and took the formatOuput code from [this answer](http://stackoverflow.com/questions/798967/php-simplexml-how-to-save-the-file-in-a-formatted-way). =\ – Sosa Nov 12 '12 at 04:26
  • We have 2012, this file is from 2004, it's time for an update: http://php.net/manual/de/class.domdocument.php – Dr.Molle Nov 12 '12 at 04:35

1 Answers1

1

Your code is unpleasant to read so I'm not sure what else is wrong with it, but the reason for your error is this line:

$dom->loadXML($songs);

$songs is an array, not a string. (Read and pay attention to error messages.)

Francis Avila
  • 31,233
  • 6
  • 58
  • 96
  • I don't understand. So for example what would I put for loadXML(); other than $songs? I tried `"playlist.xml"` and it didn't work. I get a different error `Start tag expected, '<' not found in Entity, line` – Sosa Nov 12 '12 at 03:43
  • Read [the documentation](http://php.net/manual/en/domdocument.loadxml.php)--`$source` should be a string containing XML. And there is a similar method [`load()`](http://www.php.net/manual/en/domdocument.load.php) which loads from a file. – Francis Avila Nov 12 '12 at 14:31