我有一个这样的 xml 文件:(这是动态创建的 xml 文件,类别不是静态的)
<data>
<step id="1">
<category id="js"></category>
<category id="css"></category>
<category id="xml"></category>
</step>
<step id="2">
<category id="js"></category>
<category id="css"></category>
<category id="xml"></category>
<category id="php"></category>
<category id="html"></category>
</step>
<step id="3">
<category id="xml"></category>
<category id="php"></category>
<category id="html"></category>
</step>
</data>
我想将 xml 文件转换为 javascript 对象我用 jquery 做
$.get('data.xml', function(xml){
var oXML = $(xml).find('data'),
data = {};
oXML.each(function(){
var stepID = jQuery(this).attr('id');
data[stepID] = {};
jQuery(this).each(function(){
var categoryID = jQuery(this).attr('id')
data[stepID][categoryID] = 'is available';
});
});
});
结果看起来像这样
obj = {
1: {
js: 'is available',
css: 'is available',
xml: 'is available'
},
2: {
js: 'is available',
css: 'is available',
xml: 'is available',
php: 'is available',
html: 'is available'
},
3: {
xml: 'is available',
php: 'is available',
html: 'is available'
}
}
但我希望在所有步骤中都有所有类别。你知道我该怎么做吗?我想要这样的结果对象
obj = {
1: {
js: 'is available',
css: 'is available',
xml: 'is available',
php: 'is not available',
html: 'is not available'
},
2: {
js: 'is available',
css: 'is available',
xml: 'is available',
php: 'is available',
html: 'is available'
},
3: {
js: 'is not available',
css: 'is not available',
xml: 'is available',
php: 'is available',
html: 'is available'
}
}