Jump to content

Recommended Posts

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.

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.