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><b>Jan Entry ID 1</b></id>
<date>entry date</date>
<time>entry time</time>
<title>entry title</title>
<body>entry <b>body</b>! 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><b>Jan Entry ID 2</b></id>
<date>entry date</date>
<time>entry time</time>
<title>entry title</title>
<body>entry <b>body</b>! 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><b>Feb Entry ID 1</b></id>
<date>entry date</date>
<time>entry time</time>
<title>entry title</title>
<body>entry <b>body</b>! 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><b>Feb Entry ID 2</b></id>
<date>entry date</date>
<time>entry time</time>
<title>entry title</title>
<body>entry <b>body</b>! 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><b>Jan Entry ID 1</b></id>
<date>entry date</date>
<time>entry time</time>
<title>entry title</title>
<body>entry <b>body</b>! 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><b>Jan Entry ID 2</b></id>
<date>entry date</date>
<time>entry time</time>
<title>entry title</title>
<body>entry <b>body</b>! 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><b>Feb Entry ID 1</b></id>
<date>entry date</date>
<time>entry time</time>
<title>entry title</title>
<body>entry <b>body</b>! 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><b>Feb Entry ID 2</b></id>
<date>entry date</date>
<time>entry time</time>
<title>entry title</title>
<body>entry <b>body</b>! 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>
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: