xml help

djmartinaku22

New Member
Messages
1
Reaction score
0
Points
0
I want to make a "Message of the day" panel for my portal, but for some reason i can't get the xml file to load using javascript.

This is my parser code.
Code:
function loadXMLDoc("news.xml")
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else // IE 5/6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET","news.xml",false);
xhttp.send();
xmlDoc=xhttp.responseXML; 
}
This is the code i used to call the function and create the text by getting the "title" tag.
Code:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue
Can someone please help me out? It'll be greatly appreciated. Thank you.
 

cybrax

Community Advocate
Community Support
Messages
764
Reaction score
27
Points
0
You do know that ActiveX only works on Internet Explorer, so it is very likely that almost half your visitors will not know the message of the day even exists. Those with IE may also be reluctant to run the ActiveX popup.

Better to do it all with javascript, as few people have this disabled in their web browsers.

Here's all the code you need just cut and paste..
http://www.javascriptkit.com/script/script2/motivatequotes.shtml
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Code:
function loadXMLDoc("news.xml")
{
    [...]
    xhttp.open("GET","news.xml",false);
    xhttp.send();
    xmlDoc=xhttp.responseXML; 
}
AJAX is asynchronous (that's what the first A stands for), which means send() returns before the task finishes, at which point the response isn't ready. Set an onreadystatechange handler to handle the response:
Code:
    xhttp.open("GET","news.xml",false);
    xhttp.onreadystatechange=function(evt) {
        if (xhttp.readyState == xhttp.DONE && xhttp.status < 400) {
            // note: [URL="http://c2.com/cgi/wiki?GlobalVariablesAreBad"]globals[/URL] are bad. Find an alternative
            window.xmlDoc=xhttp.responseXML
        }
    }
    xhttp.send();

You do know that ActiveX only works on Internet Explorer, so it is very likely that almost half your visitors will not know the message of the day even exists. Those with IE may also be reluctant to run the ActiveX popup.
XMLHttpRequest is supported natively on all major, modern browsers, and only requires ActiveX on IE 5-6. It's not supported on some mobile browsers.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Also, if you are going to call the function:

Code:
xmlDoc=loadXMLDoc("books.xml");

You should define the function with a parameter and a return value.

Code:
function loadXMLDoc( url )
{
...
xhttp.open("GET", url ,false);

...

xhttp.responseXML;

Lastly, when are you calling all of this? If you put the call in the <head> section, it will be called before all the body elements are created. So, you might be trying to load your message of the day into a non-existent div.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
AJAX is asynchronous (that's what the first A stands for), which means ...
Code:
xhttp.open("GET","news.xml",[B]false[/B]);
    xhttp.onreadystatechange=function(evt) {
        if (xhttp.readyState == xhttp.DONE && xhttp.status < 400) {
            // note: globals are bad. Find an alternative
            window.xmlDoc=xhttp.responseXML
        }
    }
    xhttp.send();


If you set the asynchronous flag to false, as both you and OP did, you don't need the callback.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
If you set the asynchronous flag to false, as both you and OP did, you don't need the callback.
D'oh, you're right. I didn't look closely enough at the call to open.

OP: note that synchronous requests can severely impact the responsiveness of your page, depending on how long the server takes to respond. On the major browsers, Javascript executes in a single thread per tab, so a synchronous call will block all JS execution on a page until a response is received. Use asynchronous calls–your users won't thank you for it, but they also won't complain, which is more important.

As for figuring out why your code isn't working, try an interactive debugger, such as firebug ([2], [3]). With a debugger, you can pause script execution at any line in the script and inspect the value of variables.
 
Last edited:

phantrungson198751

New Member
Messages
2
Reaction score
0
Points
0
AJAX is asynchronous (that's what the first A stands for), which means send() returns before the task finishes, at which point the response isn't ready. Set an onreadystatechange handler to handle the response:
Code:
    xhttp.open("GET","news.xml",false);
    xhttp.onreadystatechange=function(evt) {
        if (xhttp.readyState == xhttp.DONE && xhttp.status < 400) {
            // note: [URL="http://c2.com/cgi/wiki?GlobalVariablesAreBad"]globals[/URL] are bad. Find an alternative
            window.xmlDoc=xhttp.responseXML
        }
    }
    xhttp.send();
XMLHttpRequest is supported natively on all major, modern browsers, and only requires ActiveX on IE 5-6. It's not supported on some mobile browsers.
good:cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool:
 

dozmap

New Member
Messages
2
Reaction score
0
Points
0
Thanks very nice information in this thread.This information helps to understand the xml..very nice.








Hosting
 
Top