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.

Results 1 to 2 of 2
  1. #1
    Member webmastercorey's Avatar
    Join Date
    Nov 2005
    Location
    Kansas
    Posts
    49
    Member #
    11909
    Okay, I've been tweaking some code from W3Schools and can't get it to work perfectly. The trouble arises when the parser reads through the document and discovers an element that is "blank" or "doesn't exist." Instead of reading through the rest of the xml it just stops reading it. In other words, I'll have a table that is missing half the information. Does anyone know a way that this script can just ignore elements that are empty or don't exist and continue reading... or does anyone know of a better script that does the same thing. ASP or Javascript?

    Thanks in advance... here's the code:

    Code:
                        					
    <table id="xml-table" width="490px" cellspacing="0" class="bodyText">
                          <tr> 
                            <td width="60px" align="left" class="table-heading"> 
                             Date</td>
                            <td width="250" align="left" class="table-heading"> 
                             Opponent</td>
                            <td width="60px" align="left" class="table-heading"> 
                             Site</td>
                            <td width="60px" align="left" class="table-heading"> 
                             Time</td>
    
                            <td width="60px" align="left" class="table-heading">
                             Result</td>
                          </tr>
                        <!-- Begin XML Parse -->
    					<script type="text/javascript">
    						var year = "07-08results";
                            var xmlDoc=null;
                            
                            if (window.ActiveXObject)
                            {// code for IE
                                xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                            }
                            else if (document.implementation.createDocument)
                            {// code for Firefox, Mozilla, Opera, etc.
                                xmlDoc=document.implementation.createDocument("","",null);
                            }
                            else
                            {
                                alert('Your browser cannot handle this script');
                            }
                            
                            if (xmlDoc!=null)
                            {
                                xmlDoc.async=false;
                                xmlDoc.load("schedule.xml");
                            
                                var x=xmlDoc.getElementsByTagName("game");
                                for (var i=0;i<x.length;i++)
                                { 					
    							
    								if (x[i].getElementsByTagName("tournament")[0].childNodes[0].nodeValue=="yes" 
    								|| x[i].getElementsByTagName("tournament")[0].childNodes[0].nodeValue=="Yes"
    								&& x[i].getElementsByTagName("format")[0].childNodes[0].nodeValue=="title"
    								|| x[i].getElementsByTagName("format")[0].childNodes[0].nodeValue=="Title") 
    								{ 
    									// tournament tr
    									document.write("<tr class='table-tournament-title'>");				
    								}
    								else if (x[i].getElementsByTagName("tournament")[0].childNodes[0].nodeValue=="yes" 
    								|| x[i].getElementsByTagName("tournament")[0].childNodes[0].nodeValue=="Yes"
    								&& x[i].getElementsByTagName("format")[0].childNodes[0].nodeValue=="Game"
    								|| x[i].getElementsByTagName("format")[0].childNodes[0].nodeValue=="game")
    								{
    									// tr
    									document.write("<tr class='table-tournament-game'>");			
    								}
    								else
    								{
    									// tr
    									document.write("<tr class='table-game'>");			
    								}
    								
    									document.write("<td align='left' valign='top' class='table-cell'>");
    									document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);
    									document.write("</td>");
    									
    									// opponent
    									document.write("<td align='left' valign='top' class='table-cell'>");
    									if (x[i].getElementsByTagName("live")[0].childNodes[0].nodeValue=="yes" 
    									|| x[i].getElementsByTagName("live")[0].childNodes[0].nodeValue=="Yes") 
    									{ 
    										document.write("<a href='/athletics/listen_live.asp' class='listen-live'>");
    										document.write(x[i].getElementsByTagName("opponent")[0].childNodes[0].nodeValue);
    										document.write("</a>");
    									}
    									else
    									{
    									document.write(x[i].getElementsByTagName("opponent")[0].childNodes[0].nodeValue);
    									}
    									document.write("</td>");
    									
    									if (x[i].getElementsByTagName("tournament")[0].childNodes[0].nodeValue=="yes" 
    									|| x[i].getElementsByTagName("tournament")[0].childNodes[0].nodeValue=="Yes"
    									&& x[i].getElementsByTagName("format")[0].childNodes[0].nodeValue=="title"
    									|| x[i].getElementsByTagName("format")[0].childNodes[0].nodeValue=="Title") 
    									{ 
    										// site
    										document.write("<td align='left' valign='top' colspan='2' class='table-cell'>");
    										document.write(x[i].getElementsByTagName("site")[0].childNodes[0].nodeValue);
    										document.write("</td>");			
    									}
    									else 
    									{
    										// site
    										document.write("<td align='left' valign='top' class='table-cell'>");
    										document.write(x[i].getElementsByTagName("site")[0].childNodes[0].nodeValue);
    										document.write("</td>");
    									}
    									
    									//time
    									document.write("<td align='left' valign='top' class='table-cell'>");
    									document.write(x[i].getElementsByTagName("time")[0].childNodes[0].nodeValue);
                                    	document.write("</td>");
    									
    									if (x[i].getElementsByTagName("tournament")[0].childNodes[0].nodeValue=="yes" 
    									|| x[i].getElementsByTagName("tournament")[0].childNodes[0].nodeValue=="Yes"
    									&& x[i].getElementsByTagName("format")[0].childNodes[0].nodeValue=="title"
    									|| x[i].getElementsByTagName("format")[0].childNodes[0].nodeValue=="Title") 
    									{ 
    									
    									}
    									else if (x[i].getElementsByTagName("tournament")[0].childNodes[0].nodeValue!="undefined")
    									{
    										// result
    										document.write("<td align='left' valign='top' class='table-cell'>");
    										document.write("<a href='");
    										document.write(year);
    										document.write("/");
    										document.write(x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue);
    										document.write("' target='_blank'>");
    										document.write(x[i].getElementsByTagName("results")[0].childNodes[0].nodeValue);
    										document.write("</a>");
    										document.write("</td>");
    									}
    									else
    									{
    										// result
    										document.write("<td align='left' valign='top' class='table-cell'>");
    										document.write("</td>");
    									}
    									
    								document.write("</tr>");
                                }
                            }
                        </script>
                        <!-- // END XML PARSE // -->
    
                        </table>

    Here is an example "schedule.xml" table with an element missing:
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <schedule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    	<game>
    		<format>Game</format>
    		<date>Nov. 1  </date>
    		<opponent>College</opponent>
    		<site>Home</site>
    		<time>8:00 pm  </time>
    		<results></results>
    		<link>college.htm</link>
    		<live>Yes</live>
    		<sac>No</sac>
    		<tournament>No</tournament>
    	</game>
    	<game>
    		<format>Game</format>
    		<date>Nov. 5 </date>
    		<opponent>College 2</opponent>
    		<site>Home </site>
    		<time>7:00 pm </time>
    		<results>W 65-52</results>
    		<link>college2.htm</link>
    		<live>Yes</live>
    		<sac>No</sac>
    		<tournament>No</tournament>
    	</game>
    </schedule>
    Here is the CSS if you want that too...
    HTML Code:
    .table-heading
    {
        height: 2px;
        background: #516f94;
        padding: 5px;
        font-family: "Arial Black";
        font-size: 12px;
        color: #fff;
    }
    
    #xml-table tr
    {
        background: #e5e9ee;
        height: 15px;
        font-family: "Arial";
        font-size: 12px;
    }
    
    .table-tournament-title td
    {
    
        height: 2px;
        padding: 5px;
        font-size: 11px;
    	font-weight: bold;
    	background: #c3cedc;
    }
    
    .table-tournament-game td
    {
        padding: 2px 2px 2px 5px;
        font-size: 11px;
    	border-right: 1px solid #c3cedc;
    	border-left: 1px solid #c3cedc;
    	border-bottom: 2px solid #c3cedc;
        background: #e7eaef;
    }
    
    .table-tournament-game
    {
        margin-top: 15px;
    }
    
    .table-game td
    {
        padding: 5px;
        border: 1px #fff solid;
    }
    
    .listen-live
    {
        background: url(./listen-live.jpg) no-repeat right top;
        padding-right: 20px;
    }
    "What good will it be for a man if he gains the whole world, yet forfeits his soul?"
    -Matthew 16:26

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    I'm uselessly lazy to read through all the code, but it sounds like it's encountering a DOMTextNode (the whitespace is still part of the DOM), and possibly assuming that it has elements that a DOMElement normally has, like a name and attributes. I can recommend that as you walk through any DOM document, you can try this kind of approach to make sure you're on a DOMElement and not a lame DOMTextNode:
    Code:
    for (var i = 0; i < node.childNodes.length; i++)
    {
        if (node.childNodes[i].nodeName)
        {
            // good bet that node is a usable DOMElement and not a DOMTextNode
        }
    }
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!


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:07 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com