I'm developing an HTML5 application.
我正在开发一个HTML5应用程序。
I want to parse an XML like this one:
我想解析像这样的XML:
<?xml version="1.0" encoding="utf-8" ?>
<cards>
...
<card id="3">
<name lang="es"></name>
<description lang="es"></description>
<name lang="en"></name>
<description lang="en"></description>
</card>
...
</cards>
I want to get the name and description that have attribute lang="en".
我想获得具有属性lang =“en”的名称和描述。
I start writing code, but I don't know how to finish it:
我开始编写代码,但我不知道如何完成它:
function loadCards(lang)
{
$.ajax({
type: "GET",
url: 'data/english.xml',
dataType: "xml",
success:parseCardsXml
});
}
function parseCardsXml(xml)
{
$(xml).find('Card').each(function()
{
var id = $(this).attr('id');
var name = $(this).find('name');
}
}
By the way, loadCards
function has an argument (or parameter) called lang
.
顺便说一句,loadCards函数有一个名为lang的参数(或参数)。
How can I pass this argument to function parserCardsXml(xml)
? How can I get name and description with a specific attribute?
如何将此参数传递给函数parserCardsXml(xml)?如何获取具有特定属性的名称和描述?
3 个解决方案
#1
13
To answer the specific questions, "How can I pass this argument to function parserCardsXml(xml)?"
要回答具体问题,“我如何将此参数传递给函数parserCardsXml(xml)?”
function loadCards(lang)
{
$.ajax({
type: "GET",
url: 'data/english.xml',
dataType: "xml",
success: function (xml) { parseCardsXml(xml, lang); }
});
}
And, "How can I get name and description with a specific attribute?"
并且,“我怎样才能获得具有特定属性的名称和描述?”
function parseCardsXml(xml, lang)
{
var $xml = $(xml),
name = $xml.find('name[lang="' + lang + '"]').text(),
desc = $xml.find('desc[lang="' + lang + '"]').text();
}
#2
5
var xml='<cards>\
<card id="3">\
<name lang="es"></name>\
<description lang="es"></description>\
<name lang="en"></name>\
<description lang="en"></description>\
</card></cards>';
and the jquery part
和jquery部分
$(xml).find('Card').each(function(i,j)
{
console.log($(j).attr("id"));
console.log($(j).find("name").attr("lang"));
});
http://www.jsfiddle.net/VZjmV/6/
http://www.jsfiddle.net/VZjmV/6/
#3
3
$(xml).find('name[lang="en"], description[lang="en"]')
should do the trick
$(xml).find('name [lang =“en”],description [lang =“en”]')应该做的伎俩
Edit: More complete answer
编辑:更完整的答案
$(xml).find('card').each(function () {
var id, name, description;
id = $(this).attr('id'); // or just `this.id`
name = $(this).children('name[lang="en"]').text();
description = $(this).children('description[lang="en"]').text();
// do something with the id, name, and description
});
#1
13
To answer the specific questions, "How can I pass this argument to function parserCardsXml(xml)?"
要回答具体问题,“我如何将此参数传递给函数parserCardsXml(xml)?”
function loadCards(lang)
{
$.ajax({
type: "GET",
url: 'data/english.xml',
dataType: "xml",
success: function (xml) { parseCardsXml(xml, lang); }
});
}
And, "How can I get name and description with a specific attribute?"
并且,“我怎样才能获得具有特定属性的名称和描述?”
function parseCardsXml(xml, lang)
{
var $xml = $(xml),
name = $xml.find('name[lang="' + lang + '"]').text(),
desc = $xml.find('desc[lang="' + lang + '"]').text();
}
#2
5
var xml='<cards>\
<card id="3">\
<name lang="es"></name>\
<description lang="es"></description>\
<name lang="en"></name>\
<description lang="en"></description>\
</card></cards>';
and the jquery part
和jquery部分
$(xml).find('Card').each(function(i,j)
{
console.log($(j).attr("id"));
console.log($(j).find("name").attr("lang"));
});
http://www.jsfiddle.net/VZjmV/6/
http://www.jsfiddle.net/VZjmV/6/
#3
3
$(xml).find('name[lang="en"], description[lang="en"]')
should do the trick
$(xml).find('name [lang =“en”],description [lang =“en”]')应该做的伎俩
Edit: More complete answer
编辑:更完整的答案
$(xml).find('card').each(function () {
var id, name, description;
id = $(this).attr('id'); // or just `this.id`
name = $(this).children('name[lang="en"]').text();
description = $(this).children('description[lang="en"]').text();
// do something with the id, name, and description
});