Click to See Complete Forum and Search --> : Trouble Calling up XML file


wompninja
08-20-2008, 01:42 PM
I have an xml document that I use as a news file:

<news>
<newsItem>
<theDate><![CDATA[8.4.08]]></theDate>
<smallText><![CDATA[Fluid Studio Helping to Rock-out the Gallivan Center]]></smallText>
<bigText><![CDATA[<p>Fluid Studio has teamed up with the Gallivan Center and 94.9 The Blaze to present four nights of high-energy rock 'n' roll concerts in their new Wednesday Rocks concert series in the heart of Salt Lake City. High profile national and local acts will headline the shows on August 6, 13, 20, and 27.]]></bigText>
</newsItem>
</news>

(shortened, there are a lot more newsItem)
I am trying to pull from this news.xml and put the info into my home page at
www.fluidbrandaid.com in the news box at the bottom. The box needs to contain the 3 most recent news items. Any help on this would be much appreciated!

ibechane
08-21-2008, 01:21 AM
Do you plan to pull it server-side or client-side, and what language?

When you need to start manipulating data (like ordering things), XML can become kind of annoying to use compared to an SQL database.

I'm actually a beginner as far as using XML as data storage, but I've been playing around, so maybe this will help. I'm using PHP's simplexml to handle the XML document.

First, it's easiest if you have the data in order, preferably newest at the top. Simplexml will return your xml document in a multidimensional array, so it becomes almost trivial to get data from the first three entries:


$data = simplexml_load_file("news.xml");

echo $data->newsItem[0]->theDate; //echos theDate node of the first newsItem
echo $data->newsItem[0]->bigText; //echos bigText node of the first newsItem
echo $data->newsItem[2]->smallText; //echos smallText node of the third newsItem

//etc.


If your XML is forward-chronologically ordered, i.e. newest at the end, you can use sizeof() to get the number of items, then subtract:


$data = simplexml_load_file("news.xml");
$size = sizeof($data->newsItem);
echo $data->newsItem[$size-1]->bigText; //last
echo $data->newsItem[$size-2]->bigText; //second to last
echo $data->newsItem[$size-3]->bigText; //third to last

//keep in mind that sizeof gives a cardinal number of elements, and array indices start with zero.


If your XML file is not chronologically ordered, then I'm not sure how you'd order them without having to do fancy stuff to the array, which seems like a big hassle.


This solution is obviously server-side. You could probably do it with Javascript, but I wouldn't suggest it, esp. if your XML file gets significantly large and yet you still only need the first 3 entries, because I think any solution would require transferring the entire XML file.

Hope that helps.

wompninja
08-21-2008, 11:17 AM
First of all thanks for responding, I never get any replies on this site and I don't know why. Your idea seems like it will work a lot better than anything I've been able to come up with. The xml file is chronological, the top is the newest item. Do you have a tutorial or link to Simplexml? I have access to the server and the xml file is in the same directory as the site. I am using xml because I have another site(created by someone else) that uses the same news.xml file and I want them to be sync'd. If anyone else has any suggestions that would be great. I tried to parse the xml with javascript but I can't ever get any text to fill in.

ibechane
08-21-2008, 11:53 AM
I actually only started using simplexml recently. Here's a page with useful examples: http://www.onlamp.com/pub/a/php/2004/01/15/simplexml.html

There's also the PHP manual: http://us2.php.net/simplexml

wompninja
08-21-2008, 12:03 PM
Thanks. Here is the javascript that I was trying use to call up my xml. Anybody have any idea what I would have to do to get it to work?

<script type="text/javascript">
function parseXML()
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e)
{
alert(e.message);
return;
}
}
xmlDoc.async=false;
xmlDoc.load("http://www.fluid-studio.net/xml/news.xml");

document.getElementById("news").innerHTML=
xmlDoc.getElementsByTagName("newsItem")[0].childNodes[0].nodeValue;
document.getElementById("newsItem").innerHTML=
xmlDoc.getElementsByTagName("theDate")[0].childNodes[0].nodeValue;
document.getElementById("smallText").innerHTML=
xmlDoc.getElementsByTagName("bigText")[0].childNodes[0].nodeValue;
}
</script>

ibechane
08-21-2008, 02:40 PM
Have you tried this script on IE? For some reason, Firefox gives a permission when I try to do things with XML with Javascript. It has worked in IE though..