jQuery中如何选择XML子节点? [英] How do I select xml child nodes in jQuery?
本文介绍了jQuery中如何选择XML子节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在这个code解析XML文件正常,但是,在XML文件中我有多个作者的节点,我希望能够把一个逗号在每个作者之间。在XML变化从一个到一至四个作者。谢谢你的时间提前。
/ *加载XML文件* /
$阿贾克斯({
网址:XML / AJAX响应-data.xml中,
缓存:假的,
成功:libraryXML
});
功能libraryXML(XML){
$(XML).find(书)。每个(函数(){
/ *解析XML文件* /
VAR ID = $(本).attr('身份证');
VAR检查= $(本).attr(签出)
VAR标题= $(本).find(标题)文本()。
VAR ISBN = $(本).find(ISBN-10')文本()。
VAR作者= $(本).find(作家)文本()。
/ *吐出一些书* /
$('<李类=书 - '+ ID +'检查'+检查+'>< /李>)HTML('<跨度类=ID>'+ ID +'&LT。 ; / SPAN><跨度类=头衔 - GT;+标题+'< / SPAN><跨度类=作者>+作家+'< / SPAN><跨度类= ISBN>+ ISBN +'< / SPAN>')appendTo('库');
});
}
<书籍ID =1检出=1>
<作家>
<笔者>大卫·弗拉纳根< /笔者>
< /作者:GT;
<冠军> JavaScript的:权威指南< /标题>
< ISBN-10> 0596101996< / ISBN-10 -10
< /书>
<书籍ID =2签出=1>
<作家>
<笔者>约翰Resig的< /笔者>
< /作者:GT;
<冠军>临JavaScript的技术(专业)的LT; /标题>
< ISBN-10> 1590597273< / ISBN-10 -10
< /书>
<书籍ID =3签出=0>
<作家>
<笔者>埃里希·伽马和LT; /笔者>
<笔者>理查德头盔< /笔者>
<笔者>拉尔夫·约翰逊和LT; /笔者>
<笔者>约翰·M. Vlissides< /笔者>
< /作者:GT;
<冠军>设计模式:可复用面向对象软件和其中的元素; /标题>
< ISBN-10> 0201633612< / ISBN-10 -10
< /书>
解决方案
我会改变你的code到这样的事情:
函数libraryXML(XML){
$(XML).find(书)。每个(函数(){
/ *解析XML文件* /
VAR ID = $(本).attr('身份证');
VAR检查= $(本).attr(签出)
VAR标题= $(本).find(标题)文本()。
VAR ISBN = $(本).find(ISBN-10')文本()。
VAR作者= $(本).find('作者');
/ *吐出一些书* /
$('<李>< /李>')
.addClass(书 - '+ id)的.addClass('检查'+查)
.append($('<跨度类=ID>< / SPAN>)文本(ID))
.append($('<跨度类=头衔>< / SPAN>)文本(标题))
.append($('<跨度类=作者>< / SPAN>。)文本($地图(作者,函数(作者){返回$(作者)的.text()})加入。 (,)))
.append($('<跨度类=ISBN>< / SPAN>)文本(ISBN))
.appendTo('图书馆');
});
}
的优点是,它以逗号分隔的作家,就像你想要的,但它也prevents在生成的HTML的任何XSS攻击利用的 jQuery的文本功能以HTML转义输出。
Right now this code parses the XML file fine, however, in the XML file I have multiple author nodes, I'd like to be able to put a comma in between each author. The XML varies from one to from one to four authors. Thank you ahead of time.
/* Load XML File */
$.ajax({
url: "xml/ajax-response-data.xml",
cache: false,
success: libraryXML
});
function libraryXML (xml) {
$(xml).find('book').each(function(){
/* Parse the XML File */
var id = $(this).attr('id');
var checked = $(this).attr('checked-out')
var title = $(this).find('title').text();
var isbn = $(this).find('isbn-10').text();
var authors = $(this).find('authors').text();
/* Spit out some books */
$('<li class="book-'+id+' checked'+checked+'"></li>').html('<span class="id">' + id + '</span><span class="title">' + title + '</span><span class="author">' + authors +'</span><span class="isbn">' + isbn + '</span>').appendTo('.library');
});
}
<book id="1" checked-out="1">
<authors>
<author>David Flanagan</author>
</authors>
<title>JavaScript: The Definitive Guide</title>
<isbn-10>0596101996</isbn-10>
</book>
<book id="2" checked-out="1">
<authors>
<author>John Resig</author>
</authors>
<title>Pro JavaScript Techniques (Pro)</title>
<isbn-10>1590597273</isbn-10>
</book>
<book id="3" checked-out="0">
<authors>
<author>Erich Gamma</author>
<author>Richard Helm</author>
<author>Ralph Johnson</author>
<author>John M. Vlissides</author>
</authors>
<title>Design Patterns: Elements of Reusable Object-Oriented Software</title>
<isbn-10>0201633612</isbn-10>
</book>
解决方案
I'd change your code to something like this:
function libraryXML (xml) {
$(xml).find('book').each(function(){
/* Parse the XML File */
var id = $(this).attr('id');
var checked = $(this).attr('checked-out')
var title = $(this).find('title').text();
var isbn = $(this).find('isbn-10').text();
var authors = $(this).find('authors');
/* Spit out some books */
$('<li></li>')
.addClass('book-'+id).addClass('checked'+checked)
.append($('<span class="id"></span>').text(id))
.append($('<span class="title"></span>').text(title))
.append($('<span class="author"></span>').text($.map(authors, function(author){ return $(author).text() }).join(', ')))
.append($('<span class="isbn"></span>').text(isbn))
.appendTo('.library');
});
}
The advantages are that it does the comma-separated author, like you wanted, but it also prevents any XSS attacks in the generated HTML by using jQuery's text function to HTML-escape the output.
这篇关于jQuery中如何选择XML子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文