Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-20-2009, 08:19 PM   PM User | #1
ptmuldoon
Regular Coder

 
Join Date: Feb 2005
Posts: 660
Thanks: 5
Thanked 14 Times in 14 Posts
ptmuldoon is on a distinguished road
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>

Last edited by ptmuldoon; 10-22-2009 at 03:35 PM..
ptmuldoon is offline   Reply With Quote
Old 10-20-2009, 09:25 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,687
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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');
}); 
__________________
Fumigator is offline   Reply With Quote
Old 10-22-2009, 02:27 PM   PM User | #3
ptmuldoon
Regular Coder

 
Join Date: Feb 2005
Posts: 660
Thanks: 5
Thanked 14 Times in 14 Posts
ptmuldoon is on a distinguished road
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 is offline   Reply With Quote
Old 10-22-2009, 04:29 PM   PM User | #4
ptmuldoon
Regular Coder

 
Join Date: Feb 2005
Posts: 660
Thanks: 5
Thanked 14 Times in 14 Posts
ptmuldoon is on a distinguished road
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?

Last edited by ptmuldoon; 10-22-2009 at 04:49 PM..
ptmuldoon is offline   Reply With Quote
Old 10-22-2009, 05:01 PM   PM User | #5
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,687
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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.
__________________
Fumigator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:24 AM.


Advertisement
Log in to turn off these ads.