0

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?

LChaos2
  • 167
  • 4
  • You are not getting an empty string. `...nodeValue.toString()[0].charCodeAt(0) = 10` which is carriage return not empty. – jrook Apr 11 '17 at 03:05
  • @jrook Thank you! This allows me to traverse through the nodes. Is there a way to get the node values? I understand that `var total = xmlDoc.children[0].children[0];` is the child node, but how do I get the value out of it? – LChaos2 Apr 11 '17 at 03:07
  • By using nodeValue? `.children[0].childNodes[0].nodeValue ="5"` – jrook Apr 11 '17 at 03:13
  • the node **total** does not have any children. It has childNodes though. – jrook Apr 11 '17 at 03:16
  • @jrook Thank you very much! I managed to get what I needed. – LChaos2 Apr 11 '17 at 03:31
  • Glad to help. Please consider accepting my answer (and up-voting it!) if it helped you solve your problem. Thanks! – jrook Apr 11 '17 at 05:27

2 Answers2

1

The string is not empty.

console.log(xmlDoc.getElementsByTagName("modules")[0].childNodes[0].nodeValue.charCodeAt(0));

10

which is the ASCII code for carriage return. You can take a look here to find out more about childNodes and children. xmlDoc.children will give you an HTML collection which you can traverse and extract information from.

Note: I suggest editing the title of the question to be more helpful for those who may see it in the future.

Community
  • 1
  • 1
jrook
  • 3,459
  • 1
  • 16
  • 33
0

As per requirement please try below code.

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"};

var xml = response.body;

var xmlDoc = new DOMParser().parseFromString(xml, 'text/xml');

var allChildNodes = xmlDoc.documentElement.childNodes;

var total = allChildNodes[1].textContent;

alert(total);
Kaushik Andani
  • 1,252
  • 1
  • 14
  • 22