SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parsing XML using javascript

    Have this XML:
    http://www.austinmetrobaseball.com/gamedata.xml

    and need javascript to write this using document.write:
    Code:
    <div id="gamedate">Monday, Apr 16</div> <!-- gamedate --> 
    <div id="gamescore"> 
      <div class="table-row"> 
        <div class="col1L">Nationals</div> 
        <div class="col2L">2</div> 
        <div class="col3L"></div> 
        <div class="col4L">(1-1-1)</div> 
      </div> <!-- table-row --> 
      <div class="table-row"> 
        <div class="col1W">Lynx</div> 
        <div class="col2W">8</div> 
        <div class="col3L"></div> 
        <div class="col4W">(2-1-0)</div> 
      </div> <!-- table-row -->
    </div>  <!-- gamescore -->

    I will be iterating through each division in the XML and building the gamescore
    div and it's children for each game node in the division.

    This is to be on an interval, so when the division changes, I will re-build the

    data for the selected division.

    Need a function to build the above HTML from the XML for each game node in

    the selected division.


  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HFT must be having problems - I can't access their site and so I haven't been able to reply to your question.

    You can't use "document.write" after the page has already loaded.

    I will try to look into this when I can. Perhaps someone else here has a suggestion.

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a quick demo. It doesn't transform the xml to html the way you want but it shows one way to parse the xml.

  4. #4
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeFoster View Post
    HFT must be having problems - I can't access their site and so I haven't been able to reply to your question.
    Yeah, I saw it was having slowdown problems, too.

    Quote Originally Posted by MikeFoster View Post
    You can't use "document.write" after the page has already loaded.
    I am seeing that I really need to write ALL the gamedata
    nodes and not do them dynamically based on the division.
    I will have to "pause" the gameinfo timer while the division is
    changing. If you look at the convoluted js from the old
    scroller, it does this.

    Quote Originally Posted by MikeFoster View Post
    I will try to look into this when I can. Perhaps someone else here has a suggestion.
    I took a look at your demo and I may be able to use it along
    with some of the old scroller code.

  5. #5
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can't figure why the error???

    See comment below about error... Any ideas?

    Code:
          var XHR = new xHttpRequest();
          var SERVER_DATA = 'http://www.austinmetrobaseball.com/gamedata.xml';
          var gZIndex = 1; // global
          window.onload = function()
          {
            if (XHR.busy) {
              alert('A response is pending.');
            } else {
              var f = xGetElementById('idRequest1');
              var div = 'Austin';
              if (!XHR.send('GET', SERVER_DATA, 'Austin', 2000, 'rnd', true, div, OnResponse)) {
                xGetElementById('idResponse1').innerHTML = '<p>Request Error (' + XHR.err.name + '): ' + XHR.err.message + '<\/p>';
            }
            }
            return false; // don't allow form to submit
          }      
          
          function OnResponse(req, status, division)
          {
            if (status == XHR.OK) {
              var i, d = xGetElementsByTagName('division', req.responseXML);
              for (i = 0; i < d.length; ++i) {
                if (av(d[i], 'name') == division) {
                  var j;
                  var g = xGetElementsByTagName('game', d[i]);
                  for (j = 0; j < g.length; ++j) {
                    var s = '';
                    var vscore = xNodeAttrib(g[j], 'vscore');
                    var hscore = xNodeAttrib(g[j], 'hscore');        
                    if (vscore > hscore) {
                      vclass1= 'col1W'; vclass2= 'col2W';  vclass3= 'col3L';  vclass4= 'col4W';
                      hclass1= 'col1L';  hclass2= 'col2L';  hclass3= 'col3L';  hclass4= 'col4L';
                    } else {
                      vclass1= 'col1L';  vclass2= 'col2L';  vclass3= 'col3L';  vclass4= 'col4L';
                      hclass1= 'col1W';  hclass2= 'col2W';  hclass3= 'col3L';  hclass4= 'col4W';
                    }
                    /* I am getting Expected ';' error on line 45 -- THIS IS LINE 45!!! */
                    s += "<di" + v id=\"gamedate\">" + xNodeAttrib(g[j], 'gamedate');
                    s += "<\/div>");
                    s += "  <di" + v class="table-row">");
                    s += "    <di" + v class=\""+vclass1+"\">";
                    s += xNodeAttrib(g[j], "visitor");
                    s += "<\/div>");
                    s += "    <di" + v class=\""+vclass2+"\">";
                    s += vscore;
                    s += "<\/div>");
                    s += "    <di" + v class=\""+vclass3+"\"><\/div>");
                    s += "    <di" + v class=\""+vclass4+"\">("
                    s += xNodeAttrib(g[j], "vwins")+"-"+xNodeAttrib(g[j], "vlosses")+"-"+xNodeAttrib(g[j], "vties")+")";
                    s += "<\/div>");
                    s += "  <\/div>"); 
                    s += "  <di" + v class=\"table-row\">");
                    s += "    <di" + v class=\""+hclass1+"\">";
                    s += xNodeAttrib(g[j], "home");
                    s += "<\/div>");
                    s += "    <di" + v class=\""+hclass2+"\">";
                    s += hscore;
                    s += "<\/div>");
                    s += "    <di" + v class=\""+hclass3+"\"><\/div>");
                    s += "    <di" + v class=\""+hclass4+"\">("
                    s += xNodeAttrib(g[j], "hwins")+"-"+xNodeAttrib(g[j], "hlosses")+"-"+xNodeAttrib(g[j], "hties")+")";
                    s += "<\/div>");
                    s += "  <\/div>"); 
                  }
                  break; // out of 'for'
                }
              }
            }
            else {
              if (status & XHR.TIMEOUT) {
                s = '<p>Timeout Error<\/p>';
              }
              if (status & XHR.NOXMLCT) {
                s = '<p>XML content-type expected but received: ' + req.getResponseHeader('Content-Type') + '<\/p>';
              }
              if (status & XHR.RSPERR) {
                s = '<p>Response Error (' + req.status + '): ' + req.statusText + '<\/p>';
              }
            }
            xGetElementById('gameinfo').innerHTML = s;
          }
          function xNodeAttrib(node, attribname)
          {
            return node.attributes.getNamedItem(attribname).value;
          }

  6. #6
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    You there??

    Hey, Mike, can you take a look at my last post? I'm still getting that error and I cannot, for the life of me, figure out what is causing it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •