Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    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.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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');
    }); 

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    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> 

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    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.

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •