开发者

Is there a more efficient way to parse this XML using jQuery?

开发者 https://www.devze.com 2023-04-02 15:03 出处:网络
function xmlPa开发者_如何学Gorser(xml, projectName) { var currentIndex = 0; $(xml).find(\'entry\').each(function(){
function xmlPa开发者_如何学Gorser(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?


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


You could try using entry[projectName=...] selector, and have jquery search for the right entry (see: http://api.jquery.com/attribute-equals-selector/ ) however you could have problems with case sensitivity.

When you need the next project, you could use $project.next() (see : http://api.jquery.com/next/ ), same for previous.


Instead of using the find function you can use the prev() and next() functions. Try this out:

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

You can also pass a selector to those to make sure it only returns the prev/next you want like this:

.prev('entry');


If you're concerned about efficiency, you can try something like this. Once you get a list of 'entry' elements, there's no need to keep searching for them again - everything you need is in the array.

function xmlParser(xml, projectName) {

    var entries = $(xml).find('entry');
    var previous;
    var project;
    var next;

    $.each(entries, function(index, element)) {
        if (element.attr('projectName').toLowerCase() == projectName) {
            if (index > 0) {
                previous = entries[index - 1];
            } else {
                previous = null;
            }
            project = entries[index];
            if (index < entries.length - 1) {
                next = entries[index + 1];
            } else {
                next = null;
            }
        }
    });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消