Solved

JQUERY AND XML

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article discusses how to create an extensible mechanism for linked drop downs.
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)

763 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