Guess it's a simple question for a javascript guru, but i'm learning and got a problem I can't guess out.
I'm interested in reading an XML file using javascript. Here is an example of the XML file:
<object name='obj1'>
<attribute name='att1' value='val1'/>
<attribute name='att2' value='val2'/>
<attribute name='attN' value='valN'/>
<subobject name='sub1'>
<attribute name='satt1' value='sval1'/>
<attribute name='satt2' value='sval2'/>
<attribute name='sattN' value='svalN'/>
</subobject>
<subobject name='subn'>
<attribute name='snatt1' value='snval1'/>
<attribute name='snatt2' value='snval2'/>
<attribute name='snattN' value='snvalN'/>
</subobject>
</object>
As you can see, I have N objects. Each object has global attributes, and may have 0...M subobjects with their attributes.
Problem is, applying this (my) code to parse ob1 attributes, I also get all subobject attributes:
if (window.XMLHttpRequest) {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET",URL,false);
xmlhttp.send();
var xmlDoc=xmlhttp.responseXML;
var objList = xmlDoc.getElementsByTagName("object");
var attrList = objList[0].getElementsByTagName("attribute"); // Got ALL attributes here
}
In that code (no error test for simplicity), my attrList object gets both obj1 and all subobject attributes.
How can I rewrite the code to get only att1...attN?!?!
Thanks in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…