Solved

JQUERY AND XML

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

Highfive Gives IT Their Time Back

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

Suggested Solutions

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

760 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

19 Experts available now in Live!

Get 1:1 Help Now