Jump to content

php - xml


Hooch

Recommended Posts

I was hoping someone could look at my php code here. I need the following line of code to be in one line (due to childNodes in actionscript)

print "<song name=".$songs->name." band=".$songs->band." File=".$songs->File." />\n";

Here's the whole code..

<?php header ("content-type: text/xml"); print "<?xml version=\"1.0\"?>\n"; mysql_connect("localhost", "*****", "****") or die ("<b>Unable to establish connection with MySQL.</b>\n"); mysql_select_db("****") or die ("<b>Unable to locate specified database.</b>"); $query = "SELECT * FROM music ORDER BY track_id ASC"; $result = mysql_query($query); while($songs = mysql_fetch_object($result)) {  print "<songs>\n";print "<song name=".$songs->name." band=".$songs->band." File=".$songs->File." />\n";print "</songs>\n"; } ?>

But it comes up with an error. Anyone see what's wrong??

The XML page cannot be displayed Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. --------------------------------------------------------------------------------A string literal was expected, but no opening quote character was found. Error processing resource 'http://www.@@@@.c/s...<song name=song01 band=band01 File=music/song01.mp3 />-----------^

Thanks for taking the time to look and help. Hooch

Link to comment
Share on other sites

You need to include quotes around your properties. I assume you want to create this:<song name="name" band="band" File="file" />So you need to use this:print "<song name=\"".$songs->name."\" band=\"".$songs->band."\" File=\"".$songs->File."\" />\n";

Link to comment
Share on other sites

Hey man, thanks. Tha't what need's to be displayed. Thank you. But a new error is now up..

The XML page cannot be displayed Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. --------------------------------------------------------------------------------Only one top level element is allowed in an XML document. Error processing resource 'http://www.@@@.com<songs>-^

hmm..??

Link to comment
Share on other sites

You need to wrap your entire document with another element. In this document here:

<element1>  <element2a />  <element2b />  <element2c />  <element3>    <element4 />  <element3></element1>

The top-level element is element1. You can't have more than one top-level element, so this is illegal:

<element1a>  <element2a />  <element3>    <element4 />  <element3></element1a><element1b>  <element2b />  <element2c /></element1b>

Just wrap your entire document with a parent element to make the parser happy.

Link to comment
Share on other sites

FYI This code works..

print "<songs>\n";     print "<song>".$songs->song."</song>\n";     print "<artist>".$songs->artist."</artist>\n";print "<mp3>".$songs->mp3."</mp3>";         print "</songs>\n";

But because of childNodes in my actionscript I need them all to be on one line. I followed a tut for the actionscript and I havent' grasped the nodes yet to edit that instead.

Link to comment
Share on other sites

OK, I'm not an XML guru or anything, I was only going off the error messages. So the first error message was easy, that was about quotes. You were originally producing an XML sheet that had elements like this:<song name=song01 band=band01 File=music/song01.mp3 />The error occured because it was expecting this:<song name="song01" band="band01" File="music/song01.mp3" />The only difference is the quotes.So the second error is about top-level elements. Apparently Actionscript only wants one top-level element (again, I'm not an XML guru so I don't know if that's true for all XML docs or just the ones Actionscript has to deal with). The top-level element in a document is anything that is not inside another element. So in this example:

<element1>  <element2 /></element1>

element2 is a child node, and element1 is the parent node. Since there are no other nodes, element1 is also the top-level node (there is nothing above it). You can change that by wrapping the whole thing in another node:

<element3>  <element1>    <element2 />  </element1></element3>

Now element3 is the top-level node, element1 is a child of element3, and element2 is a child of element1 (grandchild of element3).So, you are producing a list of songs. It will probably look something like this:

<song name="name1" band="band1" File="file1" /><song name="name2" band="band2" File="file2" /><song name="name3" band="band3" File="file3" /><song name="name4" band="band4" File="file4" />

The error about the top-level elements just says that the document can only contain 1 top-level element. In that example there are 4 (each song element). So in order to get only 1 top-level, you wrap the whole thing in another element:

<songs>  <song name="name1" band="band1" File="file1" />  <song name="name2" band="band2" File="file2" />  <song name="name3" band="band3" File="file3" />  <song name="name4" band="band4" File="file4" /></songs>

Now the <songs> element is the top-level element (the only one), and under it are 4 <song> elements.I'm 6'1", and I live in the desert, so hopefully this makes sense from one sun burnt noggin to another.

Link to comment
Share on other sites

Thanks guy, it's making sense now. I'm at work, so I cannot try this out, but should my new code be...

 <?php header ("content-type: text/xml"); print "<?xml version=\"1.0\"?>\n"; mysql_connect("localhost", "*****", "****") or die ("<b>Unable to establish connection with MySQL.</b>\n"); mysql_select_db("****") or die ("<b>Unable to locate specified database.</b>"); $query = "SELECT * FROM music ORDER BY track_id ASC"; $result = mysql_query($query); print "<music>\n";while($songs = mysql_fetch_object($result)) {  print "<songs>\n";print "<song name=\"".$songs->name."\" band=\"".$songs->band."\" File=\"".$songs->File."\" />\n";print "</songs>\n"; } print "</music>";?>

Thanks again for your help lad!!

Link to comment
Share on other sites

Everything looks fine, but you don't need that interior <songs> element since you already have the <music> element. Or you can also just change <music> to <songs>. But you want the one element (songs or music) sitting outside the while loop where music is now, and then in the while loop all you do is spit out the <song> element.

Link to comment
Share on other sites

ok great thank you. I'll update my progress once I get a change to try it out.*** I have the playlist.php producing the same info the playlist.xml does Thanks guy!! Now for some reason the mp3 player does not want to work still. I'll have to ask some actionscript guru's. Maybe something needs changing there.*** WOOHOO!! It would not work from my existing browser. Once I tried from a new browser, it worked perfect!! Thank you justsomeguy your time and effort has helped me to a tee again.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...