View Single Post
Old 09-15-2005, 11:46 AM  
psili
Confirmed User
 
Join Date: Apr 2003
Location: Loveland, CO
Posts: 5,526
I got bored and hacked a javascript solution together from some web tutorials. No need for PHP or anything.

"sample.xml" file that could be stored on a remote server:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<content>
 <item>
  <link>http://www.yahoo.com/</link>
  <text>This links to yahoo.com</text>
  <image>http://us.a1.yimg.com/us.yimg.com/i/ww/beta/y3.gif</image>
 </item>
 <item>
  <link>http://www.google.com</link>
  <text>Google is good.</text>
  <image>http://www.google.com/intl/en/images/logo.gif</image>
 </item>
</content>
And the following is an html page that will parse that XML file into HTML and place it where you want in an html page:

[html]
<html>
<head>
<title>XML Parser</title>
<script language="JavaScript">
var xmlhttp;
var output = "";
function loadXMLDoc(url)
{
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
xmlhttp.onreadystatechange=xmlhttpChange
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
if (xmlhttp)
{
xmlhttp.onreadystatechange=xmlhttpChange
xmlhttp.open("GET",url,true)
xmlhttp.send()
}
}
}


function xmlhttpChange()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyStatehahahaha4)
{
// if "OK"
if (xmlhttp.statushahahaha200)
{
xmlObj = xmlhttp.responseXML;
parseXML(xmlObj);
}
else
{
alert("Problem retrieving XML data")
}
}
}

function parseXML(xmlObj)
{
items = xmlObj.getElementsByTagName("item");
for(i=0;i<items.length;i++)
{
link = items[i].getElementsByTagName("link")[0].firstChild.nodeValue;
text = items[i].getElementsByTagName("text")[0].firstChild.nodeValue;
image = items[i].getElementsByTagName("image")[0].firstChild.nodeValue;
output += "<p><a href=\"" + link + "\">"
+ "<img src=\""+image+"\" align=left>"
+ text
+ "</a></p><hr/>";
}
document.getElementById("targetText").innerHTML = output;
}
</script>
</head>
<body onLoad="loadXMLDoc('sample.xml')">
<p>Output from xml document:</p>
<div id="targetText">
<!-- output goes here -->
</div>
</body>
[/html]
__________________
Your post count means nothing.
psili is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote