Solved

JQUERY AND XML

Posted on 2013-02-05
4
304 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Index on a Table 6 15
jQuery Scroll To Top 5 17
Setting up JqueryUI with MVC 5 in Visual Studio 5 8
VBScript on Html 15 26
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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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)

825 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