How do I get the full value(including any additional tags) from .text of an XML key / Value using Javascript

So I’ve been stumped on a particular problem.

I’ve got an XML file which contains a key value pair.

My basic goal is to just enter a key and the corresponding value is to be displayed using an alert box.

My XML file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<name key="amit"><person><b1>Amit</b1></person> </name>
<name key="Peter">Morris </name>

My Script file is as follows:

var xmlDoc;
function getValue(key)
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
if(xmlDoc.readyState == 4)
   for(var i=0;i<xmlDoc.documentElement.childNodes.length;i++)
        return xmlDoc.documentElement.childNodes[i].text;


My html file is as follows:

    <script src="script.js"></script>
        var value=getValue("amit");

When I run my code, if the value I pass is ‘Peter’ then my alert box shows me ‘Morris’. Which I expected.

But when I pass the value ‘amit’, the alert box shows me ONLY ‘Amit’. It does not show me


I need the full value to be printed.

Can someone kindly let me know what I’ve missed or how I can go about doing this?

Source: xml

Leave a Reply