+ Reply to Thread
Results 1 to 9 of 9

Thread: xml help

  1. #1
    djmartinaku22 is offline x10Hosting Member djmartinaku22 is an unknown quantity at this point
    Join Date
    Nov 2010
    Posts
    1

    Unhappy xml help

    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.

  2. #2
    cybrax's Avatar
    cybrax is offline x10 Elder cybrax is on a distinguished road
    Join Date
    Aug 2009
    Location
    UK
    Posts
    699

    Re: xml help

    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/...tequotes.shtml
    The code must flow.
    Project 157: Latest UK Jobs direct to your mobile phone
    New Domain under construction: Lovelogic.net
    home for some new projects that we can't keep here ;)


  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: xml help

    Quote Originally Posted by djmartinaku22 View Post
    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: globals are bad. Find an alternative
                window.xmlDoc=xhttp.responseXML
            }
        }
        xhttp.send();
    Quote Originally Posted by cybrax View Post
    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.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: xml help

    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.
    Nothing is always absolutely so.

  5. #5
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: xml help

    Quote Originally Posted by misson View Post

    AJAX is asynchronous (that's what the first A stands for), which means ...
    Code:
    xhttp.open("GET","news.xml",false);
        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.
    Nothing is always absolutely so.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: xml help

    Quote Originally Posted by descalzo View Post
    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 by misson; 11-30-2010 at 05:55 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  7. #7
    phantrungson198751 is offline x10Hosting Member phantrungson198751 is an unknown quantity at this point
    Join Date
    Sep 2010
    Posts
    2

    Re: xml help

    Quote Originally Posted by misson View Post
    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: globals 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

  8. #8
    dozmap is offline x10Hosting Member dozmap is an unknown quantity at this point
    Join Date
    Dec 2010
    Posts
    2

    Re: xml help

    Thanks very nice information in this thread.This information helps to understand the xml..very nice.








    Hosting

  9. #9
    dotinfiniti29 is offline x10Hosting Member dotinfiniti29 is an unknown quantity at this point
    Join Date
    Oct 2010
    Posts
    2

    Re: xml help

    PHP has SimpleXML, which is cross platform and simple:
    http://www.w3schools.com/php/php_xml_simplexml.asp

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers