How do I access the Id
attribute of Detail
and colorName
of Color
.
For example: using x[i].getAttribute('No')
returns 0002
. How do I access Id="6"
and colorName="orange"
. Thank you.
<xml>
<Items>
<Item No="0001">
<Details>
<Detail Id="3">
<Colors>
<Color colorName="green" />
<Color colorName="yellow" />
</Colors>
</Detail>
<Detail Id="4">
<Colors>
<Color colorName="red" />
<Color colorName="blue" />
</Colors>
</Detail>
</Details>
</Item>
<Item No="0002">
<Details>
<Detail Id="5">
<Colors>
<Color colorName="purple" />
<Color colorName="pink" />
</Colors>
</Detail>
<Detail Id="6">
<Colors>
<Color colorName="grey" />
<Color colorName="orange" />
</Colors>
</Detail>
</Details>
</Item>
<Item>
<Details>
</Details>
</Item>
</Items>
</xml>
The below code returns "0002":
var x = xmlDoc.getElementsByTagName("Item");
Number = '0002';
for (i = 0; i < x.length; i++) {
if (x[i].getAttribute('No') === Number) //desc
ans = x[i].getAttribute('No') // returns 0002
}
Tried x[i].getElementsByTagName("Detail")[i].getAttribute('Id')
, but dint work