Suppose I have the following XMLHttpRequest().responseText stored in var response
:
{"success":true,"error":null,"body":"<modules>\n <total>5<\/total>\n <status>\n <id>0<\/id>\n <name>Archon<\/name>\n <portCount>14<\/portCount>\n <attached>true<\/attached>\n <\/status>\n <status>\n <id>1<\/id>\n <name>PC4<\/name>\n <portCount>0<\/portCount>\n <attached>false<\/attached>\n <\/status>\n <status>\n <id>2<\/id>\n <name>APC<\/name>\n <portCount>0<\/portCount>\n <attached>false<\/attached>\n <\/status>\n <status>\n <id>3<\/id>\n <name>SL1<\/name>\n <portCount>0<\/portCount>\n <attached>false<\/attached>\n <\/status>\n <status>\n <id>4<\/id>\n <name>SW5-1<\/name>\n <portCount>0<\/portCount>\n <attached>false<\/attached>\n <\/status>\n <status>\n <id>5<\/id>\n <name>ALC<\/name>\n <portCount>0<\/portCount>\n <attached>false<\/attached>\n <\/status>\n <status>\n <id>65<\/id>\n <name>VirtualModule<\/name>\n <portCount>16<\/portCount>\n <attached>true<\/attached>\n <\/status>\n<\/modules>\n"}
After that, I did the following to extract the body:
var json = JSON.parse(response);
var xml = json.body;
The object in var xml
is a string, so I tried to attempted to parse it into an XMLDocument:
var xmlDoc = new DOMParser().parseFromString(xml, 'text/xml');
The trouble here is I have no idea how to get to the child nodes. I have tried the following to get the value of total (which should be 5):
console.log(xmlDoc.getElementsByTagName("modules")[0].childNodes[0].nodeValue);
But all I'm getting is a blank string. I feel that this is something that should be very simple, but I'm simply not getting it. I'm even more clueless once I want to get the value of the various name tags.
How do I go about getting the values of the child nodes?