...

View Full Version : JQuery parsing xml



ptmuldoon
10-20-2009, 09:19 PM
I'm trying to learn and understand how to use jquery to parse some xml data.

I found a nice brief tutorial Here (http://think2loud.com/reading-xml-with-jquery/).

I believe I understand the code some and so how function finds the approprate 'site' or tag your looking for.

But my xml data has a number var name="xxx" for tag names, so I'm not sure who to modify the code corrrectly.

A sample of my xml looks something like:

(Note, the xml data is ALOT larger than this. I only posted a small piece to show what I'm working with.


<global>
<var name="TEAM_ABBREVIATION">JD</var>
<var name="TEAM_ID">0</var>
<var name="MAVID">10</var>
<var name="USER_ID">29</var>
<var name="TEAM_OWNERS">John Doe</var>
<var name="HELMET_LARGE">blank</var>
<var name="TEAM_BANNER">none</var>
</global>

Fumigator
10-20-2009, 10:25 PM
Much better tutorial:

http://www.xml.com/pub/a/2007/10/10/jquery-and-xml.html

In your case you want to loop through the return XML object, using a jQuery selector. For each "var" tag you find, you can determine the value of the name attribute using the attr() function. Something like this:


$('var').each(function() {
var varName = $(this).attr('name');
});

ptmuldoon
10-22-2009, 03:27 PM
Thanks for the help and other tutorial. I'm still failing at getting the information though, even after your explanation. I would have thought the below is correct, but I can't get anything to show.



<script type="application/javascript">
$(function() {
$('#update-target a').click(function() {
$.ajax({
type: "GET",
url: "test.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('var').each(function(){
var Owners = $(this).attr('TEAM_OWNERS');
$('<li></li>').html(Owners).appendTo('#update-target ol');
});
}
});
});
});
</script>

ptmuldoon
10-22-2009, 05:29 PM
Ok, I "think" I'm learning. I figured out how to get/print/show th attributes.

I learned this from another tutorial to get the attributes


<script type="application/javascript">
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "test.xml",
dataType: "xml",
success: parseXml
});
});

function parseXml(xml)
{
$(xml).find("var").each(function()
{
$("#output").append($(this).attr("name") + "<br />");
});

}
</script>

But how do you show the values of each attribute to be able to parse and show the Owner name, etc?

Fumigator
10-22-2009, 06:01 PM
That's what this does right here:


$("#output").append($(this).attr("name") + "<br />");

That is saying: Append to (add to the end of) the html element that has an id of "output". The text to add is the value in the "name" attribute of the current object (THIS object) I am looping through, and the loop is iterating through every "var" tag in the $(xml) object.

Make sure you have an element in your html (a div tag for example) with an id of "output". Without it, nothing happens. You can actually create it using jQuery:



$('body').append("<div id='output'></div>");


But it's easy enough just to put it in your html.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum