Разбор XML с JQuery с условным для атрибутов - PullRequest
1 голос
/ 01 октября 2011

Я пытался разобрать этот xml: http://henriquebarone.animatubo.com/spider/jquery/teste.xml

Написание этого кода:

    $(document).ready(function(){
    $.ajax({
        type: "GET",
        url: "teste.sif",
        dataType: "xml",
        success: Parser 
    });
});

function Parser(sif) {  
    $(sif).find('canvas').each(function(){

        canvas_name = $(this).find('name').text();
        $('<title>'+canvas_name+'</title>').appendTo('head');

        canvas_width = $(this).attr('width');
        canvas_height = $(this).attr('height');
        $('<div id="canvas" style="width:'+canvas_width+'px; height:'+canvas_height+'px; border: solid 1px black; margin: 20px auto"></div>').appendTo('body'); 

        $(this).find('layer').each(function(){
            layer_type = $(this).attr('type');
            if ( layer_type == 'import' ) {
                $(this).find('param').each(function(){
                    param_name = $(this).attr('name');
                    if ( param_name == 'filename' ) {
                        file_name = $(this).find('string').text();
                        $('<div class="import" style="width:50px; height:50px; backgound-img: url('+file_name+')"').appendTo('#canvas');
                    }

                });
            }
        });

    }); 
}

Для каждого тега <layer> с атрибутом type="import" я хочу получитьего потомок <param> с атрибутом name="filename", из которого я хочу получить текст тега <string>.

1 Ответ

1 голос
/ 01 октября 2011
$(sif).find('layer[type="import"] param[name="filename"] string')

должен найти то, что вы хотите. Я сделал jsFiddle с примером .

...