2011-07-21 65 views
2
<Types> 
    <Type id='1' name='sport'> 
     <SubTypes id='1' name='football' />  
     <SubTypes id='2' name='tennis' /> 
    </Type> 

    <Type id='2' name='education'> 
     <SubTypes id='1' name='school' />  
     <SubTypes id='2' name='university' /> 
    </Type> 
</Types> 

我有以前的XML文件,我想用Javascript解析它,我如何才能訪問特定類型的所有子類型?如何使用Javascript解析嵌套的XML文件?

我用

var x=xmlhttp.responseXML.documentElement.getElementsByTagName("Type"); 

解析類型,但我怎麼可以解析爲每種類型的子類型?

回答

1
var x=xmlhttp.responseXML.documentElement.getElementsByTagName("Type"); 
for (var i = 0; i < x.length; i++) { 
    var subTypes = new Array(); 
    var y = x[i].getElementsByTagName("SubTypes") 
    for (var j = 0; j < y.length; j++){ 
    subTypes.push(y[j]) 
    // or you could just process them here 
    } 

    // process the subtypes in this type 

} 

這應該讓你有... IDK的你想與這些亞型做雖然