Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

JQUERY AND XML

i NEED TO SIMPLY HAVE CODE CYCLE THROUGH XML FILE AND PRODUCE A LIST OF VALUES THE VALUES WOULD BE Batters in a Baseball game lineup I need jquery to work in the big 3 browsers.The ID is actually the name for image file for that player so 3019 would be 3019.jpg How would I create a table with image and player stats from the XML File.
http://images.sportsnetwork.com/MLB/attheballpark/players/"+player_id+".jpg
http://images.sportsnetwork.com/MLB/attheballpark/players/5548.jpg
<!DOCTYPE HTML>
  <html>
  <head>    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
 
  <script type="text/javascript">
   $(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "http://www.sportsnetwork.com/xml/mlb/scores/real/33011.xml",  // put here instead, whatever you xml file is called.
    dataType: "xml",
    success: parseXml
  });
});
   
   
    function parseXml(xml)
{
  //find every Tutorial and print the author
   var count = 100;
        for (var i=0; i < count; i++){
       
  $(xml).find("Player").each(function() {
        
           
      var fname = $(this).attr("FirstName");
      var lname = $(this).attr("Lastname");
        var pos = $(this).attr("Pos");
        var ID =$(this).attr("ID")
      document.writeln(fname  + " "  + lname + " " + pos + " " + ID);
      
        document.writeln(i);
   
      });
   }
 
   
   
   
   
    </script>
  </head>
  <body>
  <p><img name="img" src="" width="73" height="81" alt=""></p>
  </body>
</html>
0
powerztom
Asked:
powerztom
  • 2
  • 2
1 Solution
 
Tom BeckCommented:
I could not get it to work with the XML at the link so I created a local copy (attached).

Try this version of the page and script:
<!DOCTYPE HTML>
<html>
<head> 
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
  $.ajax({
    type: "GET",
    url: "GamePlayers.xml", 
    dataType: "xml",
    success: function(data){
        var players = data.getElementsByTagName("Player");
	var li = ""; var txt = ""; var img = "";
        for (var i=0; i < players.length; i++){
			txt = document.createTextNode(" " +
				players[i].getAttribute("FirstName") + " " +
				players[i].getAttribute("Lastname") + ", " +
				players[i].getAttribute("Pos")
			);
			img = document.createElement("img");
			img.src = "http://images.sportsnetwork.com/MLB/attheballpark/players/" + 
				players[i].getAttribute("ID") + ".jpg";
			li = document.createElement("li");
			li.appendChild(img);
			li.appendChild(txt);
			document.getElementById("players").appendChild(li);
		}
	}
  });
});
</script>
<style type="text/css">
#players {
	list-style:none
}
#players li {
	line-height:30px
}
#players li img {
	height:100px
}
</style>
</head>
  <body>
  	<ul id="players"></ul>
  </body>
</html>

Open in new window

GamePlayers.xml
0
 
powerztomAuthor Commented:
No fucking way. Thank You I'd buy you a six pack if you lived by me. You saved the day. God bless you.
0
 
powerztomAuthor Commented:
Un Believable incredible Thank You!
0
 
Tom BeckCommented:
I think I'd like Sam Adams Winter Lager this week. Thanks for the points.

(I don't believe I've ever seen "No f**king way" and "God bless you" used in the same post before.)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now