Forum Moderators: open

Message Too Old, No Replies

A little help writing xml

         

k1rk

11:23 am on May 8, 2008 (gmt 0)

10+ Year Member



I have set up a flash file to read from data from an xml file which is working fine. However now I want to edit the xml file from a php/html front end, using text input fields to write to it.

I have it pretty much sorted, but I cant get it to write to the xml file in the exact syntax I require for the flash file to run.

Heres how my xml file needs to read.

<?xml version="1.0"?>
<mySlideShow>
<myImage>
<myTitle>Example 1</myTitle>
<myDescription><![CDATA[<a href="http://www.link1.php" target="_self">Text and link 1</a>]]></myDescription>
<myMedia>torah.jpg</myMedia>
</myImage>
<myImage>
<myTitle>Example 2</myTitle>
<myDescription><![CDATA[<a href="http://www.link2.php" target="_self">Text and link 2</a>]]></myDescription>
<myMedia>fastride.jpg</myMedia>
</myImage>
<myImage>
</mySlideShow>

This is part of my php file to write to the xml page.

$write_string = "<mySlideShow>";
foreach($mySlideShow_final as $myImage){
$write_string .= "<myImage>";
$write_string .= "<myTitle>$myImage[myTitle]</myTitle>";
$write_string .= "<myDescription>$myImage[myDescription]</myDescription>";
$write_string .= "<myMedia>$myImage[myMedia]</mymedia>";
$write_string .= "</myImage>";
}
$write_string .= "</mySlideShow>";
$fp = fopen("images.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Project inserted or deleted successfully :)</em><br />";
print "<a href=\"index.php\" title=\"return\">Return</a>";
?>

Now this doesnt seem to be quite right, as when run it fails to fill out data into the xml file, Im just left with an xml file with a pile of empty nodes.

Now I also need to the description to include the CDATA tag but I have no idea how to write these.

Could anyone help me out?

cmarshall

11:44 am on May 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld!

I would eschew the manual writes completely, and use something like DOMDocument [us2.php.net] or SimpleXML [us2.php.net] to gather and create the file streams.

I always create a schema [w3schools.com] for my XML, and use a tool like Synchro Software <oXygen/> to test the output. A schema will be ahuge help here. It will help you to pinpoint the problem.

httpwebwitch

12:11 pm on May 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



empty nodes: I'm thinkin' you need quotes around your array key identifiers.

before:
$myImage[myMedia]
after:
$myImage['myMedia']

you can write a CDATA tag the same way you wrote the other stuff:
$write_string .= "<![CDATA[" . $myImage['description'] . "]]>";

Here's a situation where cmarshall and I disagree - the DOMDocument library is excellent, but I'd only bother delving into it if the XML gets really complex. You're doing fine building the XML in a simple for-loop of 5 lines, so good onya.

cmarshall

12:32 pm on May 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll agree with httpwebwitch's disagreement ;)

I tend to write pretty complex XML, and my oldest [very complex] one is manual writes (which is why I try not to do that these days).

Mea culpa. Carry on.

However, I will stick to my comment about schema...

k1rk

12:49 pm on May 8, 2008 (gmt 0)

10+ Year Member



Hey fantastic feedback thank you, Im going to try the quotes tomorrow. I have read a little into SimpleXML but theres a lot to learn I do however appreciate the feed back and suggestions as it gives me plenty to look into and work on. While I had heard of Oxygen I had never really considered it so perhaps thats something I could look into if I cant resolve this myself.

Thank you both.

k1rk

12:55 am on May 9, 2008 (gmt 0)

10+ Year Member



The quotation marks made no difference and I tried a few other options to no avial. My latest code is this;

$write_string .= "<?xml version="1.0" encoding="iso-8859-1"?>\n";

$write_string .= "<myslideshow>\n";
foreach( $mySlideShow_final as $myImage)
{
$write_string .= "<myImage>\n";
$write_string .= "<mytitle>".$myImage[myTitle]."</mytitle>\n";
$write_string .= "<mydescription>".$myImage[myDescription]."</mydescription>\n";
$write_string .= "<myMedia>".$myImage[myMedia]."</myMedia>\n";
$write_string .= "</myImage>\n";
}

$write_string .= "</myslideshow>";

$fp = fopen("images.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Project inserted or deleted successfully :)</em><br />";
print "<a href=\"index.php\" title=\"return\">Return</a>";
?>

Now for some reason or another it will fill the last entry, but leave any entrys before empty.

So I end up with a xml file that looks like this (If just two entrys have been submitted)

<?xml version="1.0" encoding="iso-8859-1"?>
<mySlideShow>
<myImage>
<myTitle></myTitle>
<myDescription></myDescription>
<myMedia></myMedia>
</myImage>
<myImage>
<myTitle>test2</myTitle>
<myDescription>this is the second and last entry</myDescription>
<myMedia>test2</myMedia>
</myImage>
</mySlideShow>

I am going to tackle the CDATA issue once I have this working. Why is it only saving the last entry?

cmarshall

12:12 pm on May 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am going to tackle the CDATA issue once I have this working. Why is it only saving the last entry?

Offhand, I'd say it's probably a PHP issue, not an XML one.

Do a print_r ( $mySlideShow_final ), and make sure it's the way you expect it.

httpwebwitch

1:18 pm on May 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was just about to suggest the same thing.

foreach($mySlideShow_final as $myImage){
implies that $mySlideShow_final is an array, thus $myImage will temporarily become $mySlideShow_final[0],$mySlideShow_final[1],$mySlideShow_final[2], etc.

then inside the loop you're reading the value of $myImage[myDescription]
that implies that the original array has a structure like
$mySlideShow_final[0]['myTitle']
$mySlideShow_final[0]['myDescription']
$mySlideShow_final[1]['myTitle']
$mySlideShow_final[1]['myDescription']
etc.

k1rk

4:04 am on May 12, 2008 (gmt 0)

10+ Year Member



I thought that might be the problem. I didnt really understand how the structure worked so the example was a big help thanks httpwebwitch.

I have this all sorted now. Thank you both.