Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display remote dynamic xml wihout refresh?

Status
Not open for further replies.

rammi07

Programmer
Aug 1, 2006
20
NL
Hi all . I want to create a page that it pulls dynamic xml data from a remote server (in this case it is from radio station rss(xml) that inncludes artist name,song name and ...) and display it page. Here is example of remote rss xml:


CODE
- <playlist>
- <song>
<artist>Artisname1</artist>
<name>SongName1</name>
<image>./song_images/artistpic1.jpg</image>
<rating>3</rating>
<songid>2624</songid>
<totalvotes>35</totalvotes>
</song>
- <song>
<artist>Artistname2</artist>
<name>SongName2</name>
<image>./song_images/artistpic2.jpg</image>
<rating>3.5</rating>
<songid>4795</songid>
<totalvotes>14</totalvotes>
</song>

</playlist>

I want the page that displays above information have the updated data at all time without refresh.I be happy if some one help me achive this goal using javascript that does not produce permission error .(I want to use it on my free hosted website such as angelfire.com).Thanks
 
IE6 will give you problems with permissions using the:
Code:
var RequestHTTP = new ActiveXObject("Msxml2.XMLHTTP.4.0");
method unless your users on IE6 authorise angelfire.com as a trusted site (not freaking likely).

IE7 and firefox should be fine if you use the following:
Code:
var RequestHTTP = new XMLHttpRequest();


[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Enable Apps
 
dwarfthrower thanks for u reply. i am using the following script found one web for remote rss and i get permission error when i upload it to free sites!! But when i run it from localhost it runs corectly. I be happy if you help me fix this problem.Thanks
[/code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"<html>
<head>
<title>READING XML FILES WITH AJAX</title>
<script type="text/javascript">
// initialize XMLHttpRequest object
var xmlobj=null;
var data=new Array();
// send http request
function sendRequest(doc){
// check for existing requests
if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
xmlobj.abort();
}
try{
// instantiate object for Mozilla, Nestcape, etc.
xmlobj=new XMLHttpRequest();
}
catch(e){
try{
// instantiate object for Internet Explorer
xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
}
catch(e){
// Ajax is not supported by the browser
xmlobj=null;
return false;
}
}
// assign state handler
xmlobj.onreadystatechange=stateChecker;
// open socket connection
xmlobj.open('GET',doc,true);
// send GET request
xmlobj.send(null);
}
// check request status
function stateChecker(){
// if request is completed
if(xmlobj.readyState==4){
// if status == 200 display text file
if(xmlobj.status==200){
// create data container
createDataContainer();
// read XML data
data=xmlobj.responseXML.getElementsByTagName
('message');
// display XML data
displayData();
}
else{
alert('Failed to get response :'+ xmlobj.statusText);
}
}
}
// create data container
function createDataContainer(){
var div=document.getElementById('container');
if(div){return};
var div=document.createElement('div');
div.setAttribute('id','container');
document.getElementsByTagName('body')[0].appendChild(div);
}
// display data at a given time interval
function displayData(){
// reset data container
document.getElementById('container').innerHTML='';
var ul=document.createElement('ul');
for(var i=0;i<data.length;i++){
// create links
var li=document.createElement('li');
var a=document.createElement('a');
// assign 'href' attribute
a.setAttribute('href',data.getElementsByTagName('url')
[0].firstChild.nodeValue);
// add link labels
a.appendChild(document.createTextNode(data
.getElementsByTagName('title')[0].firstChild.nodeValue));
li.appendChild(a);
ul.appendChild(li);
}
document.getElementById('container').appendChild(ul);
// update headlines each 1 hour
setTimeout("sendRequest('./news.xml')",5*1000);
}
// execute program when page is loaded
window.onload=function(){
// check if browser is DOM compatible
if(document.getElementById&&document.
getElementsByTagName&&document.createElement){
// load XML file
sendRequest('./news.xml');
}
}
</script>
<style type="text/css">
#container {
background: #eee;
padding: 5px;
border: 1px solid #000;
}
li {
margin-top: 5px;
}
a:link,a:visited {
font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
color: #00f;
}
a:hover {
color: #f00;
}
</style>
</head>
<body>
</body>
</html>

[/code]
 
I believe I already have solved your permissions problem. If you're using IE, then you won't be able to instantiate the activeX control unless you authorise your site as a trusted site.

If you're using Firefox you won't be able to instantiate the activeX control full stop.

Have you done what I suggested?

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Enable Apps
 
so u mean i update to IE 7 only? which other lines should i change ?Thanks
 
No, if you want it to work in IE 6, you need to authorise the site as a 'Trusted Site' in your browser. That way you won't get a permissions error when you attempt to invoke the activeX control.

There are step-by-step instructions (with pictures even) on how to go about doing this at the Microsoft site:

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Enable Apps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top