Solved

pulling xml data from two different sections for one player

Posted on 2013-11-20
4
301 Views
Last Modified: 2013-11-22
I just discovered a flaw in the code I'm using and that code grabs infomation for the current play in a basketball xml game cast. This code will go and pull everything right except the Points and Assists.  In this example the xml file attached after Linescore attib values there is a players node outside the team players And this Players node That has current player play description and Points and Assists well Points and Assists are for that play and not the points and assists scored the game. And that is what I need is the points and assists for the entire game. So I'm not sure how to but match the player's team Id with Team id for entire team lineup then match the player's player Id with Player ID for entire team lineup. then grab POINTS AND ASSISTS In this attached xml file for Players node the player is Michael Carter-Williams has Players Points and Assists are both 0. But his Points and Assists under team lineup is Points = 4 and Assists = 3 that is what I need to display everything else display is perfect except Points and Assists. So Please help. Any questions just ask. Thanks EE

HERE IS SOURCE CODE
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
body {
	background-color: #000;
}
#apDiv1 {
	position: absolute;
	width: 619px;
	height: 376px;
	z-index: 10;
	left: 75px;
	top: 12px;
}
.apdivtbheader {
	color: #FFF;
}
.centermid {
	font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
	font-size: 16px;
	font-weight: bold;
	color: #000;
	text-align: center;
	vertical-align: middle;
}
.headertop {
	color: #000080;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var xmlPath = '/xml/nba/scores/real';
        var xmlGameID = '18749'; 
		$(document).ready(function(){
			$.ajax({
                    type: "GET",
                    url: xmlPath + "/" + xmlGameID + ".xml",
                    dataType: "xml",
                    success: function (data) {
                        var d = $(data);
						 d.find('Team').each(function(i,Team){
          var team = $(Team);
          var vh = team.attr('vh'); // A or H...
          var sHomeOrAway = vh == 'H' ? 'home' : 'away';
          $('#txt' + sHomeOrAway).html(team.attr('name'));
		  if ($(Team).attr('vh') == "H") {
                        //convert the XML into a jQuery object
 $('#imgcourt').attr('src', 'http://dev.sportsnetwork.com/aspdata/nhl2/NBA/Images/Courts/' + team.attr('ID')  + '.png');
           // getStats(player,txt,img,li);
           
							 }
        });
					
                   
                    }
                });	
	 });	
	
		//}

function loadData() {
 $.ajax({
                    type: "GET",
                    url: xmlPath + "/" + xmlGameID + ".xml",
                    dataType: "xml",
                    success: function (data) {
                        var d = $(data);
						 var Play = d.find('Play');
						  var msg;
						  var inn = Play.attr('TimeLeft');
						 var playtype1 = Play.attr('PlayType');
      msg = Play.find('narrative').attr('text');
                            $('#txtgameplay').html(msg);   
							$('#txttimeleft').html(inn);
                             $('#txtplaytype').html(playtype1);  
                        
                        //convert the XML into a jQuery object
gameData = $(data);

//Get the Players node
players = gameData.find('Players');

//Now loop through each Player in the Players node
$('Player', players).each(function(i, player) {
	//Output some of the attributes for this player
	
               // if (player.size() > 0) {
                                
                             //$.each(players,function(i,player) {
          player=$(player);
        var tid = player.attr('TeamID');
         var pid =  player.attr('ID');  
		 var pts =  player.attr('Points')
		 var assists = player.attr('Assists')
		  $('#txtpoints').html(pts);
          //  $("#players").append(li);
			$('txtassists').html(assists);
             $('#imgcenter').attr('src', 'http://dev.sportsnetwork.com/aspdata/nhl2/NBA/Images/NBAlOGOSZ/'+ tid +".png");
           
			
			$('#imgplayer').attr("src","http://images.sportsnetwork.com/bask/nba/atthecourt/players/" + pid + ".jpg");
           var txt = player.attr("Firstname") + " " +
                player.attr("Lastname") + ", " +
                player.attr("Position");
           // getStats(player,txt,img,li);
           $('#txt1').html(txt);
							// }
     //   });

                    });
                    }
                });	
}
</script>
</head>

<body>
<table width="750" border="0">
  <tr>
    <td><div align="center">
      <div id="apDiv1">
        <table width="650" border="0">
          <tr class="headertop">
            <td width="121" bgcolor="#FFFFFF"><div align="center" id="txt1"></div></td>
            <td width="114" bgcolor="#FFFFFF"><div align="center"><strong>TimeLeft</strong></div></td>
            <td width="122" bgcolor="#FFFFFF"><div align="center"><strong>PlayType</strong></div></td>
            <td width="120" bgcolor="#FFFFFF"><div align="center"><strong>Points</strong></div></td>
            <td width="120" bgcolor="#FFFFFF"><div align="center"><strong>Assits</strong></div></td>
          </tr>
          <tr class="apdivtbheader">
            <td height="52" class="centermid"><p><img src="Images/misc/invisibleplayer.png" name="imgplayer" width="50" height="50" id="imgplayer"></p></td>
            <td class="centermid" id="txttimeleft"></td>
            <td class="centermid" id="txtplaytype"></td>
            <td class="centermid" id="txtpoints"></td>
            <td class="centermid" id="txtassists"></td>
          </tr>
        </table>
        <table width="120" border="0">
          <tr>
            <td><p class="centermid"><img src="Images/misc/invisibleteamlogo.png" name="imgcenter" width="133" height="117" id="imgcenter"></p></td>
          </tr>
</table>
        <table width="263" border="0">
          <tr>
            <td height="18" id="txtgameplay"><p class="centermid" id="txtgameplay">&nbsp;</p>
              <p class="centermid">&nbsp;</p></td>
          </tr>
    </table>
        
      </div>
    <img src="Images/Courts/096.png" width="625" height="375" id = "imgcourt"></div></td>
  </tr>
</table>
<script>
var myVar=setInterval(function(){myTimer()},1000);

function myTimer()
{

loadData();

}
</script>
</body>
</html>

Open in new window

Court3.html
18672.XML
0
Comment
Question by:powerztom
  • 2
  • 2
4 Comments
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39662705
Not entirely sure what you mean, as you're XML structure seems a little odd. Your code doesn't really make a whole lot of sense either. What happened to the code I'd helped you with previously - you seem to have reverted back to your own, which makes 2 separate calls for the XML data, and on the second call assigns the resulting data to 2 different variables:

d = $(data);
gameData = $(data);

Can't really see why you're doing this.

Does the Players node only ever have 1 player in it. Do you want to get this player, and then grab his details from the Individual Stats node and use them?

Need a bit of a clearer explanation of what you want
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39662837
Have a look at this. I think it's what you need. No comments this time - you should be able to figure it out - there's nothing new in here ;)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Q_28299226 // Chris Stanyon</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	loadData();
});	
	
function loadData() {
	var xmlPath = '/xml/nba/scores/real';
	var xmlGameID = '18672'; 

	$.ajax({
		url: xmlPath + '/' + xmlGameID + '.xml',
		dataType: "xml"
	}).done(function(data) {
		gameData = $(data);

		play = $('Play', gameData);
		currentPlayer = $('Players Player', gameData);
		playerStats = $('Individual_Statistics Player', gameData).filter( function() { return $(this).attr('ID') == currentPlayer.attr('ID') });

		$('#playerName').html( currentPlayer.attr('Firstname') + ' ' + currentPlayer.attr('Lastname') + ', ' + currentPlayer.attr('Position') );
		$('#headShot').html( $('<img>').attr('src', 'http://images.sportsnetwork.com/bask/nba/atthecourt/players/' + currentPlayer.attr('ID') + '.jpg') );
		$('#timeLeft').html( play.attr('TimeLeft') );
		$('#playType').html( play.attr('PlayType') );
		$('#points').html( playerStats.attr('Points') );
		$('#assists').html( playerStats.attr('Assists') );
		$('#narrative').html( $('narrative', play).attr('text') );
		
		setTimeout(loadData, 3000);
	});
}
</script>
<style type="text/css">
	thead tr { background-color: #0099CC; }
	th, td { border-color: #DDDDDD; border-style: solid; border-width: 1px; padding: 5px; text-align: center;  }
</style>
</head>
<body>
		
<table>
	<thead>
		<tr>
			<th id="playerName"></th>
			<th>Time Left</th>
			<th>Play Type</th>
			<th>Points</th>
			<th>Assists</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td id="headShot"></td>
			<td id="timeLeft"></td>
			<td id="playType"></td>
			<td id="points"></td>
			<td id="assists"></td>
		</tr>
		<tr>
			<td colspan=5 id="narrative"></td>
		</tr>
	</tbody>
</table>
		
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:powerztom
ID: 39663032
Perfect Chris exactly they way I need it. You are a EE Top Gun!
0
 

Author Comment

by:powerztom
ID: 39669740
Chris,

Need to hijack your brain for a few minutes. Automatic Horizontal scrolling scoreboard tables for NHL Daily Games.

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28301645.html
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now