Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    Member #
    27055
    I'm trying to access an XML file on an external server:
    'http://www.exactscores.com/SoccerLivescoreXml.xml'

    I'd like to be able to access info inside the XML tags and place the data on my website in a creative manner. I've come up with some code to grab the data, but it is sent as a text file, not an xml. Is there some code I can use to access the xml data by referencing the tags, or to download the file and save it temporarily on my server as an xml file?

    Here's the code I've used so far:

    <html>
    <head>
    <script type="text/javascript">
    function loadXMLDoc()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp .responseText;
    }
    }
    xmlhttp.open("GET","http://www.exactscores.com/SoccerLivescoreXml.xml",true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>

    <h2>Scores</h2>
    <button type="button" onclick="loadXMLDoc()">Request data</button>
    <div id="myDiv"></div>

    </body>
    </html>

  2.  

  3. #2
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    Code:
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    
    xmlCode = xmlHttp.responseXML.documentElement; // this is the line you'll want to change. 
    }
    }
    xmlCode in the code I posted above will give you access to the document itself, as opposed to just the text output. This will allow you to parse through all the elements and nodes and all that good stuff.

    Of course, that's a whole load of fun that you're going to have to go through yourself and see how far you get. The line I posted above will get you started, though.
    inplaytoday likes this.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    Member #
    27055
    Quote Originally Posted by TheGAME1264, post: 201505
    Code:
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    
    xmlCode = xmlHttp.responseXML.documentElement; // this is the line you'll want to change.
    }
    }
    xmlCode in the code I posted above will give you access to the document itself, as opposed to just the text output. This will allow you to parse through all the elements and nodes and all that good stuff.

    Of course, that's a whole load of fun that you're going to have to go through yourself and see how far you get. The line I posted above will get you started, though.

    Thanks for the help , I've changed the code and now it looks like this:


    <html>
    <head>
    <script type="text/javascript">
    function loadXMLDoc()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()

    {

    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    xmlCode = xmlHttp.responseXML.documentElement; // this is the line you changed
    }

    }
    xmlhttp.open("GET","http://www.exactscores.com/SoccerLivescoreXml.xml",true);
    xmlhttp.send();
    }
    </script>
    </head>

    <body>
    <h2>Scores</h2>
    <button type="button" onclick="loadXMLDoc()">Request data</button>
    <div id="myDiv"></div>
    </body>
    </html>


    I am very willing to dive in and figure out how to manipulate the XML from here, but can you give me a pointer or two in the right direction? For instance, what would I put in the body to access the <SoccerLivescore> --> <Item> --> <COUNTRY> data (how to access specific ones?) and then print it to the page.

    Also, do you know a good link that would explain how to manipulate that data and print it to the page?

  5. #4
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    Here's a tutorial that will explain how you can extract the various nodes and their data:

    http://www.w3schools.com/dom/dom_nodes_get.asp

    Basically, you'll want to get the relevant Elements By Tag Name (there's a subtle hint there ) and loop through them. So for your particular example, you'll want to get the relevant COUNTRY elements by Tag Name.

    If you want to access specific ones, the easiest way would be using if conditions. Mind you, it's not necessarily the best way, depending on how many specific countries you want to extract and how. But what I've given you should give you a general idea.

    http://bytes.com/topic/javascript/an...tract-data-xml <-- this guy had a similar problem to yours and they explained it to him as well. So you may want to take bits and pieces from it.
    inplaytoday likes this.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  6. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    Member #
    27055
    Thanks for another informative answer, TheGame.

    What I'm having trouble with is understanding the difference between accessing nodes from an XML file on an external server. All of the examples I can find are based on websites that have the XML file on a local server.

    Is it possible to copy an xml file from an external server into a new xml file on your server, that would update every few minutes? Would that make it easier to access/manipulate the nodes?

  7. #6
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    You don't actually have to "copy" the file as such. That's where the xmlCode I gave you comes into play. it's an exact copy of the XML document on the remote server. This is hard stuff, though, so you're not supposed to understand it.

    Maybe a small bit of code will help you along. Once xmlCode has been assigned, do something like this:
    Code:
    var countryTags = xmlCode.getElementsByTagName ("Country");
    alert (countryTags.length);
    Once you do that, you should see the number of country tags that are actually in that XML document, which is all stored in your xmlCode. From there, you can go through them and extract stuff:
    Code:
    for (var i = 0; i < countryTags.length; i++) {
        alert (countryTags[i].firstChild.data);
    }
    Hopefully this will shed enough light to take you further.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  8. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    Member #
    27055
    aaarrghh, what am i doing wrong????
    I've implemented your suggestions as best I can, but as many ways as I've tried it, can't get the alert box to pop up and give me the countryTags.length.

    Here's my most recent effort, loadXMLDoc() should load when the page opens, and then a button should call the function showCountry() for the alert box stating number of <COUNTRY> tags. However, when I click the button, nothing happens!

    HTML Code:
    <html>
    <head>
    <script type="text/javascript">
    function loadXMLDoc()
    {
    var xmlhttp;
    
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    
    xmlCode = xmlHttp.responseXML.documentElement; // this is the line you'll want to change.
    }
    }
    
    xmlhttp.open("GET","http://www.exactscores.com/SoccerLivescoreXml.xml",true);
    xmlhttp.send();
    }
    
    function showCountry()
    {
    var countryTags = xmlCode.getElementsByTagName("COUNTRY");
    alert (countryTags.length);
    }
    
    </script>
    </head>
    
    <body onload="loadXMLDoc()">
    
    <h2>Scores</h2>
    
    <button type="button" onclick="showCountry()">Number of Countries</button>
    
    </body>
    </html>

  9. #8
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    "COUNTRY" is CaSe-SeNsItIvE. So make sure you say ...byTagName ("Country"), just as it is in the XML doc.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  10. #9
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    Member #
    27055
    I've changed the tag to reflect that in the xml file, but it is still failing to fetch it. I've uploaded the code above here:

    http://www.inplaytoday.awardspace.biz/helloo.html

    After clicking the button, an alert should pop up w/number of countries. But nothing happens, it doesn't seem to have the data at all.

  11. #10
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    Wow...never seen that before.

    Your problem isn't really code (I don't think). It's a permissions issue, and I'm guessing it has something to do with the fact that the XML file is on another site (i.e. it's a form of cross-site scripting, which poses security issues).

    Try saving the XML file onto the same server the site is on and then retrieving it that way. If it works, you'll need to find a way to get the XML onto your server whenever you need/want it.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:14 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com