bagpiperdude90 Posted June 6, 2007 Share Posted June 6, 2007 Hi, I have an XML file I'm importing, and it needs to preserve the whitespace. Is there a way to do this in SimpleXML? I am having PHP open and display an XML file that shows song lyrics. Obviously, I need the paragraphs to remain intact, or else everything runs together. And I cannot edit the XML file, as a program uses it. Also, how would the below code work out in DOMXML? I couldn't for the life of me, figure out DOM. <?php $song = 'song.xml'; $xml_file = file_get_contents($song); $xml = new SimpleXMLElement($xml_file); echo $xml->title . "<br><br>"; echo $xml->lyrics; ?> The XML file looks like this: <?xml version="1.0" encoding="UTF-8"?> <song> <title>Song Name</title> <lyrics> Verse one Verse two Verse three Chorus </lyrics> </song> I need the whitespace between the verses to stay, when it displays in HTML. Quote Link to comment https://forums.phpfreaks.com/topic/54474-solved-preserving-whitespace-in-simplexml/ Share on other sites More sharing options...
Lumio Posted June 6, 2007 Share Posted June 6, 2007 echo nl2br($xml->lyrics); Quote Link to comment https://forums.phpfreaks.com/topic/54474-solved-preserving-whitespace-in-simplexml/#findComment-269440 Share on other sites More sharing options...
bagpiperdude90 Posted June 6, 2007 Author Share Posted June 6, 2007 You are my hero! :-* Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/54474-solved-preserving-whitespace-in-simplexml/#findComment-269458 Share on other sites More sharing options...
bagpiperdude90 Posted June 6, 2007 Author Share Posted June 6, 2007 One problem - in the XML file, it has a space before certain lines (very important). How can I get PHP to show those spaces before the lines? Quote Link to comment https://forums.phpfreaks.com/topic/54474-solved-preserving-whitespace-in-simplexml/#findComment-269514 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 You know that examples can help. I would think that the \t operator may be what you are looking for, but I am unsure. Quote Link to comment https://forums.phpfreaks.com/topic/54474-solved-preserving-whitespace-in-simplexml/#findComment-269516 Share on other sites More sharing options...
bagpiperdude90 Posted June 6, 2007 Author Share Posted June 6, 2007 the site is at: http://ecpcsm.lumeriagroup.net index.php file: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <p>Instructions:<br /> This web interface has all the songs we have in the database. You can edit the songs by clicking on them, making the changes, and then pressing the "save" button. (Not implemented yet)</p> <p>To create a song, click here. (Not implemented yet) </p> <p>Below is a list of the songs in the OpenSong database (yes, I know they need to be alphabetized):</p> <p> <?php if ($handle = opendir('Songs')) { //sum up all the files in the directory while (false !== ($file = readdir($handle))) { //filter out the "." and ".." if ($file != "." && $file != ".." && $file != "_cache") { echo "<a href='showsong.php?song=$file'>"; echo ($file); echo "</a><br><br>"; } } closedir($handle); } ?> </p> </body> </html> showsong.php file: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?php $song = "Songs/" . $_GET['song']; //$song = "place.xml"; $xml_file = file_get_contents($song); $xml = new SimpleXMLElement($xml_file); echo "<b>" . $xml->title . "</b>" . "<br><br>"; echo "Presentation order:<br>" . $xml->presentation . "<br>(If empty, then program runs straight through)<br><br>"; echo nl2br($xml->lyrics); ?> </body> </html> Sample XML file: <?xml version="1.0" encoding="UTF-8"?> <song> <title>Isaiah 43</title> <author></author> <copyright></copyright> <presentation>v1 c v2 c</presentation> <ccli></ccli> <capo print="false"></capo> <key></key> <aka></aka> <key_line></key_line> <user1></user1> <user2></user2> <user3></user3> <theme></theme> <tempo></tempo> <time_sig></time_sig> <lyrics>[v1] When you pass through the waters, I will be with you And the waves, will not overcome you Do not fear, for I have redeemed you I have called you by name, you are Mine [c] For I am the Lord your God I am the Lord your God I am the Holy One of Israel, your Savior I am the Lord (Do not fear) [v2] When you pass through the fire, you'll not be hurt And the flames will not consume you Do not fear, for I have redeemed you I have called you by name, you are Mine</lyrics></song> You can see all the whitespace, and the spaces before each line of lyrics. I need those preserved. As you can see in the website, it doesnt do that for me. The song files can be accessed under /Songs/[filename].xml Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/54474-solved-preserving-whitespace-in-simplexml/#findComment-269549 Share on other sites More sharing options...
bagpiperdude90 Posted June 7, 2007 Author Share Posted June 7, 2007 Got it... thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/54474-solved-preserving-whitespace-in-simplexml/#findComment-269706 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.