我在解析XML时需要一些帮助。 XML如下:
<?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>
正如您在此配置文件中看到的那样,有两个“游戏”。每个都有自己的title属性。我已设法选择这些属性并将其添加到下拉菜单中,用户可从中选择要播放的游戏。
我的问题是,当我解析文档时,它会解析两个游戏,而不仅仅是我想要的游戏。
目前我的代码是:
$(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");
}
我知道在开始时我需要添加一些与查找游戏属性相关的内容,其中标题与组合框中选择的匹配。
如果有人可以告诉我如何选择或有选择地解析文档中“游戏标题=老年痴呆症”并忽略任何其他游戏(及其子女)的部分,我可以更改所有变量以匹配那些在组合框中的人......
感谢您的时间,并提前致谢!
答案 0 :(得分:1)
假设你有一个XMLDoc,你可以这样做:
$(xml).find("game[title=Dementia] cat").each(...
获取标题为cat
的{{1}}元素下的所有game
元素。
答案 1 :(得分:1)
试试这段代码:
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