Parsing xml with jquery with conditional for attributes
I was tryng to parse this xml: http://henriquebarone.animatubo.com/spider/jquery/teste.xml
Writing this code:
$(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');
}
});
}
});
});
}
For each <layer>
tag with the attribute type="import"
, I want to get its child <param>
with the attribute name="filename"
, from which I want the text of the <string>
tag.
$(sif).find('layer[type="import"] param[name="filename"] string')
should find what you want. I made a jsFiddle with an example.
精彩评论