我正在尝试遍历XML并获取label
节点的set
属性并创建列表,但我无法正确遍历。有人可以解释如何遍历和创建列表吗?
此处是Fiddle
XML
<chart caption='View by Law Category' subcaption='Law Category' xAxisName='null' yAxisName='Count' bgColor='AAFFAA,FFFFFF' exportEnabled='1' exportAtClient='1' exportHandler='fcExporter1' showLabels='0' showValues='1' showLegend='1' legendBorderAlpha='0' legendBgAlpha='0' legendShadow='0' formatNumberScale='0'>
<set label='1495cat' value='2' link='j-loadImpactChart-Law Category:1495cat' />
<set label='Category Test' value='35' link='j-loadImpactChart-Law Category:Category Test' />
<set label='Labour' value='149' link='j-loadImpactChart-Law Category:Labour' />
<set label='New Law 1' value='58' link='j-loadImpactChart-Law Category:New Law 1' />
<set label='testing' value='4' link='j-loadImpactChart-Law Category:testing' />
</chart>
我试过了alert($('chart>set').attr('label'));
$($xml).each(function(){
alert($(this).find("chart>set").attr('label'));
});
答案 0 :(得分:0)
这是你在找什么?
var xml = "<chart caption='View by Law Category' subcaption='Law Category' xAxisName='null' yAxisName='Count' bgColor='AAFFAA,FFFFFF' exportEnabled='1' exportAtClient='1' exportHandler='fcExporter1' showLabels='0' showValues='1' showLegend='1' legendBorderAlpha='0' legendBgAlpha='0' legendShadow='0' formatNumberScale='0'><set label='1495cat' value='2' link='j-loadImpactChart-Law Category:1495cat' /><set label='Category Test' value='35' link='j-loadImpactChart-Law Category:Category Test' /><set label='Labour' value='149' link='j-loadImpactChart-Law Category:Labour' /><set label='New Law 1' value='58' link='j-loadImpactChart-Law Category:New Law 1' /><set label='testing' value='4' link='j-loadImpactChart-Law Category:testing' /></chart>";
var xmlDoc = $.parseXML(xml)
$(xmlDoc).find("chart > set").each(function(){
alert($(this).attr('label'));
});