Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6728195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:08:26+00:00 2026-05-26T10:08:26+00:00

I have the following JQuery code that parse an XML file just they way

  • 0

I have the following JQuery code that parse an XML file just they way it should. Below it you may also find the XML file that it parses. As you can see, the root node is called GetArchive followed by another node called Y2012.

Y2012 is a year node and it may contain all 12 months of the year. Each month may contain many entries and each entry may contain many comments.

My question is how can I change my code so that I don’t have to hard code it like I am below? How can I make it so my code would check how many year nodes exist and parse each of them just like I am doing below?

Thanks a bunch for any help.

$.get('getArchive.xml', null, function (data) {

    // PARSE 2012 JANUARY NODES.

               var ul = $('<ul/>').fadeIn(1000).appendTo('#GetArchive');

       var JanEntries = $(data).find('GetArchive Y2012 Jan Entry').each( function(){

    var JanEntryId    = $(this).find("id").text();    ul.append('<li>' + JanEntryId    + '</li>');  
    var JanEntryDate  = $(this).find("date").text();  ul.append('<li>' + JanEntryDate  + '</li>');   
    var JanEntryTime  = $(this).find("time").text();  ul.append('<li>' + JanEntryTime  + '</li>');  
    var JanEntrytitle = $(this).find("title").text(); ul.append('<li>' + JanEntrytitle + '</li>');  
    var JanEntryBody  = $(this).find("body").text();  ul.append('<li>' + JanEntryBody  + '</li>');  

    $(this).find("Comment").each(function () { 
    var CommentId     = $(this).find("cid").text();   ul.append('<li>' + CommentId   + '</li>');
    var CommentDate   = $(this).find("cdate").text(); ul.append('<li>' + CommentDate + '</li>');
    var CommentTime   = $(this).find("ctime").text(); ul.append('<li>' + CommentTime + '</li>');
    var CommentFrom   = $(this).find("cfrom").text(); ul.append('<li>' + CommentFrom + '</li>');
    var CommentBody   = $(this).find("cbody").text(); ul.append('<li>' + CommentBody + '</li>');  
    });  

});

    // PARSE 2012 FEBRUARY NODES.

               var ul = $('<ul/>').fadeIn(1000).appendTo('#GetArchive');

       var JanEntries = $(data).find('GetArchive Y2012 Feb Entry').each( function(){

    var FebEntryId    = $(this).find("id").text();    ul.append('<li>' + FebEntryId    + '</li>');  
    var FebEntryDate  = $(this).find("date").text();  ul.append('<li>' + FebEntryDate  + '</li>');   
    var FebEntryTime  = $(this).find("time").text();  ul.append('<li>' + FebEntryTime  + '</li>');  
    var FebEntrytitle = $(this).find("title").text(); ul.append('<li>' + FebEntrytitle + '</li>');  
    var FebEntryBody  = $(this).find("body").text();  ul.append('<li>' + FebEntryBody  + '</li>');  

    $(this).find("Comment").each(function () { 
    var CommentId     = $(this).find("cid").text();   ul.append('<li>' + CommentId   + '</li>');
    var CommentDate   = $(this).find("cdate").text(); ul.append('<li>' + CommentDate + '</li>');
    var CommentTime   = $(this).find("ctime").text(); ul.append('<li>' + CommentTime + '</li>');
    var CommentFrom   = $(this).find("cfrom").text(); ul.append('<li>' + CommentFrom + '</li>');
    var CommentBody   = $(this).find("cbody").text(); ul.append('<li>' + CommentBody + '</li>');  
    });  

});

    // PARSE 2011 JANUARY NODES.

               var ul = $('<ul/>').fadeIn(1000).appendTo('#GetArchive');

       var JanEntries = $(data).find('GetArchive Y2011 Jan Entry').each( function(){

    var JanEntryId    = $(this).find("id").text();    ul.append('<li>' + JanEntryId    + '</li>');  
    var JanEntryDate  = $(this).find("date").text();  ul.append('<li>' + JanEntryDate  + '</li>');   
    var JanEntryTime  = $(this).find("time").text();  ul.append('<li>' + JanEntryTime  + '</li>');  
    var JanEntrytitle = $(this).find("title").text(); ul.append('<li>' + JanEntrytitle + '</li>');  
    var JanEntryBody  = $(this).find("body").text();  ul.append('<li>' + JanEntryBody  + '</li>');  

    $(this).find("Comment").each(function () { 
    var CommentId     = $(this).find("cid").text();   ul.append('<li>' + CommentId   + '</li>');
    var CommentDate   = $(this).find("cdate").text(); ul.append('<li>' + CommentDate + '</li>');
    var CommentTime   = $(this).find("ctime").text(); ul.append('<li>' + CommentTime + '</li>');
    var CommentFrom   = $(this).find("cfrom").text(); ul.append('<li>' + CommentFrom + '</li>');
    var CommentBody   = $(this).find("cbody").text(); ul.append('<li>' + CommentBody + '</li>');  
    });  

});

    // PARSE 2011 FEBRUARY NODES.

               var ul = $('<ul/>').fadeIn(1000).appendTo('#GetArchive');

       var JanEntries = $(data).find('GetArchive Y2011 Feb Entry').each( function(){

    var FebEntryId    = $(this).find("id").text();    ul.append('<li>' + FebEntryId    + '</li>');  
    var FebEntryDate  = $(this).find("date").text();  ul.append('<li>' + FebEntryDate  + '</li>');   
    var FebEntryTime  = $(this).find("time").text();  ul.append('<li>' + FebEntryTime  + '</li>');  
    var FebEntrytitle = $(this).find("title").text(); ul.append('<li>' + FebEntrytitle + '</li>');  
    var FebEntryBody  = $(this).find("body").text();  ul.append('<li>' + FebEntryBody  + '</li>');  

    $(this).find("Comment").each(function () { 
    var CommentId     = $(this).find("cid").text();   ul.append('<li>' + CommentId   + '</li>');
    var CommentDate   = $(this).find("cdate").text(); ul.append('<li>' + CommentDate + '</li>');
    var CommentTime   = $(this).find("ctime").text(); ul.append('<li>' + CommentTime + '</li>');
    var CommentFrom   = $(this).find("cfrom").text(); ul.append('<li>' + CommentFrom + '</li>');
    var CommentBody   = $(this).find("cbody").text(); ul.append('<li>' + CommentBody + '</li>');  
    });  

});    

}, 'xml');

And here is the XML file it parses.

<?xml version="1.0" encoding="utf-8"?>
<GetArchive>
  <Y2012>
    <Jan>
      <Entry>
        <id>&lt;b&gt;Jan Entry ID 1&lt;/b&gt;</id>
        <date>entry date</date>
        <time>entry time</time>
        <title>entry title</title>
        <body>entry &lt;b&gt;body&lt;/b&gt;! html friendly</body>
        <Comments>
          <Comment>
            <cid>Jan Comment ID 1</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
          <Comment>
            <cid>Jan Comment ID 2</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
        </Comments>
      </Entry>
      <Entry>
        <id>&lt;b&gt;Jan Entry ID 2&lt;/b&gt;</id>
        <date>entry date</date>
        <time>entry time</time>
        <title>entry title</title>
        <body>entry &lt;b&gt;body&lt;/b&gt;! html friendly</body>
        <Comments>
          <Comment>
            <cid>Jan Comment ID 3</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
          <Comment>
            <cid>Jan Comment ID 4</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
        </Comments>
      </Entry>
    </Jan>
    <Feb>
      <Entry>
        <id>&lt;b&gt;Feb Entry ID 1&lt;/b&gt;</id>
        <date>entry date</date>
        <time>entry time</time>
        <title>entry title</title>
        <body>entry &lt;b&gt;body&lt;/b&gt;! html friendly</body>
        <Comments>
          <Comment>
            <cid>Feb Comment ID 1</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
          <Comment>
            <cid>Feb Comment ID 2</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
        </Comments>
      </Entry>
      <Entry>
        <id>&lt;b&gt;Feb Entry ID 2&lt;/b&gt;</id>
        <date>entry date</date>
        <time>entry time</time>
        <title>entry title</title>
        <body>entry &lt;b&gt;body&lt;/b&gt;! html friendly</body>
        <Comments>
          <Comment>
            <cid>Feb Comment ID 3</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
          <Comment>
            <cid>Feb Comment ID 4</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
        </Comments>
      </Entry>
    </Feb>
  </Y2012>
  <Y2011>
    <Jan>
      <Entry>
        <id>&lt;b&gt;Jan Entry ID 1&lt;/b&gt;</id>
        <date>entry date</date>
        <time>entry time</time>
        <title>entry title</title>
        <body>entry &lt;b&gt;body&lt;/b&gt;! html friendly</body>
        <Comments>
          <Comment>
            <cid>Jan Comment ID 1</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
          <Comment>
            <cid>Jan Comment ID 2</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
        </Comments>
      </Entry>
      <Entry>
        <id>&lt;b&gt;Jan Entry ID 2&lt;/b&gt;</id>
        <date>entry date</date>
        <time>entry time</time>
        <title>entry title</title>
        <body>entry &lt;b&gt;body&lt;/b&gt;! html friendly</body>
        <Comments>
          <Comment>
            <cid>Jan Comment ID 3</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
          <Comment>
            <cid>Jan Comment ID 4</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
        </Comments>
      </Entry>
    </Jan>
    <Feb>
      <Entry>
        <id>&lt;b&gt;Feb Entry ID 1&lt;/b&gt;</id>
        <date>entry date</date>
        <time>entry time</time>
        <title>entry title</title>
        <body>entry &lt;b&gt;body&lt;/b&gt;! html friendly</body>
        <Comments>
          <Comment>
            <cid>Feb Comment ID 1</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
          <Comment>
            <cid>Feb Comment ID 2</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
        </Comments>
      </Entry>
      <Entry>
        <id>&lt;b&gt;Feb Entry ID 2&lt;/b&gt;</id>
        <date>entry date</date>
        <time>entry time</time>
        <title>entry title</title>
        <body>entry &lt;b&gt;body&lt;/b&gt;! html friendly</body>
        <Comments>
          <Comment>
            <cid>Feb Comment ID 3</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
          <Comment>
            <cid>Feb Comment ID 4</cid>
            <cdate>comment date</cdate>
            <ctime>comment time</ctime>
            <cfrom>comment from</cfrom>
            <cbody>comment body! text only</cbody>
          </Comment>
        </Comments>
      </Entry>
    </Feb>
  </Y2011>
</GetArchive>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:08:27+00:00Added an answer on May 26, 2026 at 10:08 am

    I would respectfully suggest that you rebuild the XML document, if possible. <Y2012> should be something like <Year num="2012"> and <Jan> should be something like <Month abbr="Jan" num="1">. Not only is this more consistent, but it’s easier to loop through programmatically:

    $(data).find('Year').each(function() {
        $(this).find('Month').each(function() {
            $(this).find('Entry').each(function() {
                // append id, date, time, title, body
                $(this).find('Comment').each(function() {
                    // append comment id, date, time, from, body
                });
            });
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following JQuery code that worked perfect in C#/Asp.net 2.0 to call
I have the following jQuery code that stops the user selecting text in a
Hallo, I have following jquery code for calling ASP.NET MVC controller method that is
I have the following JQuery code that processes a GET request upon page load:
I have the following code that just has controls relating to an item. <ul
Hi I have the following jQuery code that splits some content after the second
I have the following jQuery code that uses the scrollTo and localScroll plugins from
I have the following jQuery code (similar to this question ) that works in
I have the following jQuery code that works: $(document).ready(function () { $(input[id^='pos_']).change(UpdatePosition) }); function
I have the following jQuery code that finds out the width of container and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.