我想读取假设subTag1的名称和id的值,目前我能够读取TAG1的属性但不能读取subTag1的属性。我使用以下代码:
<TAG1>
<name ='tag1',id='ad'>
<subTAG1>
<name ='abc',id='123'>
<name ='asdfbc',id='143'>
</subTag1>
</TAG1>
$.ajax({
type: "GET",
url: dataset,// url from where I am reading xml
dataType: "xml",
async: false,
crossDomain: true,
headers: {
},
success: function (xml) {
$(xml).find('TAG1').each(function () {
var dsid = $(this).attr('id');
var dsname = $(this).attr('name');
console.log("Dataset id ="+dsid);
console.log("Dataset name ="+dsname);
});
答案 0 :(得分:1)
您的XML无效:
所以解析的xml应该是:
<TAG1>
<name value='tag1' id='ad'/>
<subTAG1>
<name value='abc' id='123'/>
<name value='asdfbc' id='143'/>
</subTAG1>
</TAG1>
在你的JavaScript + JQuery代码中,TAG1元素没有属性,你应该寻找一个“name”标签来获取属性:
success:
function (xml) {
$(xml).find('name').each(
function () {
var dsid = $(this).attr('id');
var dsname = $(this).attr('name');
console.log("Dataset id ="+dsid);
console.log("Dataset name ="+dsname);
});
}