XML使用jQuery解析选定的元素
问题描述:
我需要一些帮助来解析我的XML。 XML如下:XML使用jQuery解析选定的元素
<?xml version="1.0"?>
<config>
<game title="Dementia">
<cat>
<catTitle>Mild</catTitle>
<item>mild-1</item>
<item>mild-2</item>
<item>mild-3</item>
</cat>
<cat>
<catTitle>Moderate</catTitle>
<item>Moderate-1</item>
<item>Moderate-2</item>
<item>Moderate-3</item>
</cat>
<cat>
<catTitle>Severe</catTitle>
<item>Severe-1</item>
<item>Severe-2</item>
</cat>
</game>
<game title="Risks">
<cat>
<catTitle>Low</catTitle>
<item>Low-1</item>
<item>Low-2</item>
<item>Low-3</item>
</cat>
<cat>
<catTitle>Medium</catTitle>
<item>Medium-1</item>
<item>Medium-2</item>
<item>Medium-3</item>
</cat>
<cat>
<catTitle>High</catTitle>
<item>High-1</item>
<item>High-2</item>
</cat>
</game>
</config>
正如你可以在这个配置文件中看到有两个“游戏”。每个都有自己的标题属性。我设法选择这些属性并将它们添加到用户选择他们想要玩哪个游戏的下拉菜单中。
我的问题是,当我解析文档时,它解析了两个游戏,而不仅仅是我想要的。
目前我的代码是:
$(xml).find("cat").each(function(idx, v) {
categoryArrays[idx] = [];
$(v).find("catTitle").each(function(){
//$(xml).find('title:contains("Dimentia")').each(function(){
numberofCategories = numberofCategories+1;
tmpCategory = $(this).text();
categoryNames.push(tmpCategory);
var $cat = $('<div id=catPileBox>' +
'<div class="catPileBoxTitle font">' + tmpCategory + '</div>' +
'<div class="catPileBoxBody font"> </div>' +
'</div>')
.fadeIn('slow').appendTo('#catPile').attr('categoryName', tmpCategory).attr('categoryNum', idx).droppable({
containment: '#content',
stack: '#catPileBox',
cursor: 'move',
drop: handleDropEvent
});
});
//ITEM PARSING
$(v).find("item").each(function(i , vi) {
categoryArrays[idx].push($(vi).text());
numberofItems = numberofItems+1;
tmpItem = $(this).text();
//Add Item to Array
itemNames.push(tmpItem);
//UI CREATION and Atrribute setting
var $item = //$('<div id=itemPileBox div class=font >' + tmpItem + '</div>')
$('<div id=itemPileBox>' +
'<div class="itemPileBoxPic"><img src="icon.jpg"></div>'+
'<div class="itemPileBoxTitle font">' + tmpItem + '</div>' +
'<div class="itemPileBoxBody font">Having a fit and going mental</div>' +
'</div>')
.fadeIn('slow').appendTo('#itemPile').attr('itemName', tmpItem).attr('categoryNum', categoryArrayNum).draggable({
containment: '#content',
stack: '#itemPileBox',
cursor: 'move',
revert: true
});
console.log("Item Created: " + tmpItem + " // Item's Category:" + categoryArrayNum) ;
});
console.log('-- Category Array Num: ' + categoryArrayNum + ' contains: ' + categoryArrays[categoryArrayNum]);
categoryArrayNum++;
});
$(shuffleItems());
console.log("// ITEMS ASSIGNED TO CATEGORIES");
}
据我所知,在一开始,我将需要添加的东西做与寻找到符合标题,在组合框中选择的游戏属性。
如果有人能告诉我如何选择或选择性地解析文档中“游戏标题=痴呆症”部分并忽略任何其他游戏(及其子),我可以更改所有变量以匹配那些在组合框自己..
感谢您的时间,并提前致谢!
答
试试这个代码:
var xmlDoc = $.parseXML('<?xml version="1.0"?><config> <game title="Dementia"> <cat> <catTitle>Mild</catTitle> <item>mild-1</item> <item>mild-2</item> <item>mild-3</item> </cat> <cat> <catTitle>Moderate</catTitle> <item>Moderate-1</item> <item>Moderate-2</item> <item>Moderate-3</item> </cat> <cat> <catTitle>Severe</catTitle> <item>Severe-1</item> <item>Severe-2</item> </cat> </game> <game title="Risks"> <cat> <catTitle>Low</catTitle> <item>Low-1</item> <item>Low-2</item> <item>Low-3</item> </cat> <cat> <catTitle>Medium</catTitle> <item>Medium-1</item> <item>Medium-2</item> <item>Medium-3</item> </cat> <cat> <catTitle>High</catTitle> <item>High-1</item> <item>High-2</item> </cat> </game> </config>')
console.log($(xmlDoc).find("game[title=Dementia]"))
这是jsfiddle
答
假设你有一个xmlDoc中,你可以这样做:
$(xml).find("game[title=Dementia] cat").each(...
下得到有标题Dementia
game
元素都cat
元素。
其中'xml'从哪里来的,它已经被解析到一个XML文档? –