dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: Parse XML doc using jQuery and link to fields in the XML

  1. #1
    Join Date
    Feb 2012
    Location
    North East US
    Posts
    15

    Parse XML doc using jQuery and link to fields in the XML

    Hi,
    jQuery is still fairly new to me, so I apologize if I sound like a noob. I'm trying to parse an XML doc and return the values in to an HTML page. I can parse the XML (in all browsers but IE which is another bridge to cross) but I now need to identify each fund in the javascript by the <acronym> tag and add a unique URL to it's corresponding <fundname> in the XML once it has been parsed. The links need to live in the javascript unfortunately. Here is my code:

    XML
    <?xml version="1.0" encoding="Windows-1252" standalone="no">
    <presentvalue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <asofdate>2012-01-25</asofdate>
    <data>
    <gipsreport>
    <acronym>AECE1</acronym>
    <fundname>MFS Core Equity Composite</fundname>
    </gipsreport>
    <gipsreport>
    <acronym>AEDEM </acronym>
    <fundname>MFS Diversified Emerging Markets Equity Composite</fundname>
    </gipsreport>
    </data>


    HTML
    <html>
    <head>
    <title>JQuery Easy XML Read Example</title>
    script type="text/javascript" src="jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="readXML.js"></script>
    </head>
    <body>
    <div id="ContentArea"></div>
    </body>
    </html>



    readXML.js
    // File: readXML.js

    // Start function when DOM has completely loaded
    $(document).ready(function(){

    // Open the composites.xml file
    $.get("composites.xml",{},function(xml){

    // Build an HTML string
    myHTMLOutput = '';
    myHTMLOutput += '<table width="98%" border="1" cellpadding="0" cellspacing="0">';
    myHTMLOutput += '<th>Acronym</th><th>Fund Name</th>';

    // Run the function for each fund in the XML file
    $('gipsreport',xml).each(function(i) {
    gipsreportAcronym = $(this).find("acronym").text();
    gipsreportFundname = $(this).find("fundname").text();
    gipsreportPost = $(this).find("acronym").attr("post");

    // Build row HTML data and store in string
    mydata = BuildGipsHTML(gipsreportAcronym,gipsreportFundname,gipsreportPost);
    myHTMLOutput = myHTMLOutput + mydata;
    });
    myHTMLOutput += '</table>';

    // Update the DIV called Content Area with the HTML string
    $("#ContentArea").append(myHTMLOutput);
    });
    });



    function BuildGipsHTML(gipsreportAcronym,gipsreportFundname,gipsreportSE){

    // Check to see if their is a "post" attribute in the name field
    if ((gipsreportPost) != undefined){
    gipsreportPostHTML = "<strong>(" + gipsreportPost + ")</strong>";
    }
    else
    {
    gipsreportPostHTML = "";
    }

    // Build HTML string and return
    output = '';
    output += '<tr>';
    output += '<td>'+ gipsreportAcronym + gipsreportPostHTML + '</td>';
    output += '<td>'+ gipsreportFundname +'</td>';
    output += '</tr>';
    return output;

    }

    Any assistance would be greatly appreciated. Thanks for taking your time to look at this!

  2. #2
    Join Date
    Feb 2012
    Posts
    3

  3. #3
    Join Date
    Feb 2012
    Location
    North East US
    Posts
    15
    Thanks. I can parse the XML no problem (even in IE now). My issue is identifying each <acronym> tag and add applying a URL to it's corresponding <fundname> in the XML once it has been parsed.


    And, here's the new script I'm using the works in IE in case anyone else can use it if you're not applying URLs:

    <script type="text/javascript" src="jquery-1.7.1.min.js"></script>

    <script type="text/javascript">
    $(document).ready(function(){

    function parse(document){
    $(document).find("gipsreport").each(function(){
    $("#content").append(
    '<p>Acronym: '+$(this).find('acronym').text()+
    '<br /> Fund Name: '+$(this).find('fundname').text()+
    '</p>'
    );
    });
    }

    $.ajax({
    url: 'composites.xml', // name of file you want to parse
    dataType: "xml",
    success: parse,
    error: function(){alert("Error: Something went wrong");}
    });
    });
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles