2

我正在遍历 XML。子元素之一是GroupName,它在 XML 字符串/文件中不是唯一的。因此,如果您愿意的话,我试图将这些标签的不同(唯一)出现的innertextGroupName放在一个数组中。

<GroupName></GroupName>
<GroupName></GroupName>
<GroupName>VIII</GroupName>
<GroupName>Date and signature</GroupName>
<GroupName>Date and signature</GroupName>
<GroupName>Date and signature</GroupName>
<GroupName>Date and signature</GroupName>
<GroupName>VII</GroupName>

在这种特殊情况下,GroupNames是 [空字符串]、“VII”、“VIII”和“日期和签名”。

当我使用 jQuery 来提醒 ALL 时GroupNames,都会显示:

$xmlDoc.find('GroupName').each(function () {
    alert($(this).text();
});

但是,当我尝试使用indexOf()来组成一组唯一值时,其中一个似乎被跳过了!

$xmlDoc.find('GroupName').each(function () {
$groupName = ($(this).text().length == 0) ? 'empty' : $(this).text();
    if (groupnames.indexOf($groupName) < 0) {
        groupnames.push($groupName);
    }
});

在这种情况下,我得到“空”、“VIII”和“日期和签名”。'VII'不知何故丢失了,我不知道为什么。

4

0 回答 0