CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   JQuery parsing xml (http://www.codingforums.com/showthread.php?t=180124)

ptmuldoon 10-20-2009 08:19 PM

JQuery parsing xml
 
I'm trying to learn and understand how to use jquery to parse some xml data.

I found a nice brief tutorial Here.

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.
Code:

<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 09: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:
PHP Code:

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


ptmuldoon 10-22-2009 02: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.

PHP Code:

<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 04: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
PHP Code:

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

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 05:01 PM

That's what this does right here:

PHP Code:

$("#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:

PHP Code:

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

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


All times are GMT +1. The time now is 03:18 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.