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

  • Home
  • SEARCH
  • 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 6542591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:14:46+00:00 2026-05-25T11:14:46+00:00

function xmlParser(xml, projectName) { var currentIndex = 0; $(xml).find(‘entry’).each(function(){ if($(this).attr(‘projectName’).toLowerCase() == projectName) { $previous

  • 0
function xmlParser(xml, projectName) {

var currentIndex = 0;

$(xml).find('entry').each(function(){

    if($(this).attr('projectName').toLowerCase() == projectName) {

        $previous = $(xml).find('entry')[currentIndex - 1]);
        $project = $(this);
        $next = $(xml).find('entry')[currentIndex + 1]);

        //do something with these objects..

    }
    currentIndex++;

});
}

Here is some sample code. I have an XML file full of ‘entry’ elements. Each element has a ‘projectName’ attribute.

The code basically scans the XML for a project name, like say “Magic Giraffes”, returns the XML element matching it, and also the previous & next projects. It works… but I want to know if it’s the most efficient way to do it.

See how I’m handling the $previous and $next parts? It’s calling the .find() function two more times, and then grabbing elements based on the (-1) and (+1) of the current index. Is there a more efficient way to do this?

  • 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-25T11:14:46+00:00Added an answer on May 25, 2026 at 11:14 am

    assuming that each “entry” are siblings, without other elements inbetween:

    $project = $(this);
    $previous = $project.prev();
    $next = $project.next();
    

    else

    $project = $(this);
    $previous = $project.prev('entry');
    $next = $project.next('entry');
    

    in order to completly “optimize” your code, you can use variable as often as you can:

    function xmlParser(xml, projectName) {
    
    var exp = new regexp(projectName, "gi"); 
    var $entries = $('entry', xml).filter(function() {
        return exp.test( $(this).attr('projectName'));
    });
    
    $entries.each(function(){
        $project = $(this);
        $previous = $this.prev();
        $next = $this.next();
    
        //do something with these objects..
    
        });    
    }
    

    here the “$entries” variable prevent useless access to the full document, “$this” some parsing/selecting by jquery(but that effect is limited on performance).

    edit: I did change the looping / matching method, with the help of the “filter” method. Note: if you are certain there is only one “projectname” in entries, you can get rid of the the “$.each” layer, as then “$entries” becomes “$project” directly

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say on ajax success the function: xmlParser(xml) is called and the XML response
I have this code that loads an xml file using javascript: function getXmlDocument(sFile) {
Currently i am using and have looked through this code http://us.php.net/manual/en/function.xml-set-element-handler.php#85970 How do i
i found this supergroovy function of XmlParser().parseText(...). It works fine for me without namespaces...
function main() { Hello(); } function Hello() { // How do you find out
function Obj1(param) { this.test1 = param || 1; } function Obj2(param, par) { this.test2
I have an xml feed at this url Now im trying parse the content,
I'm using an XMLparser class to convert XML into an object. The problem is
I am trying to create a sort of generic xml parser, like this: Part
I have an xml file like this: <?xml version=1.0 ?> <scketchit> <categories> <category title=MANGA

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.