Solved

JQUERY AND XML

Posted on 2013-02-05
4
308 Views
Last Modified: 2013-02-06
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
Comment
Question by:powerztom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 38858139
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
 

Author Comment

by:powerztom
ID: 38858301
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
 

Author Closing Comment

by:powerztom
ID: 38858303
Un Believable incredible Thank You!
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 38859175
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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
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)

710 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