Click to See Complete Forum and Search --> : Get content from xml as plain text


tcollogne
10-20-2006, 08:46 AM
I am trying to parse an xml string in javascript. For example the following.

<person id="1">
<name>foo</name>
<firstname>bar</firstname>
</person>
<person id="2">
<name>foo</name>
<firstname>bar</firstname>
</person>


I search for a person with a specific id. So when I find it, I want to content of the tag as plain text.
For example. if I search for person with id="1", I want to get the following string "<name>foo</name><firstname>bar</firstname>". So not only the values between the tags, but also the tags.

This is the code I use



var doc;


if (window.ActiveXObject) {
doc=new ActiveXObject("Microsoft.XMLDOM");
doc.async="false";
doc.loadXML(result);
}

else {
var parser=new DOMParser();
doc=parser.parseFromString(result,"text/xml");
}

var xmlDoc=doc.documentElement;

var cells = xmlDoc.getElementsByTagName(beginTag)

for (var i = 0; i < cells.length; i++) {
id= cells[i].getAttribute("id");
if ( id == "1") {
cell = cells[i];
}
}




So how can I get the plain text from the cell variable?

Is this possible?

Thank you.

tcollogne
10-20-2006, 09:16 AM
I didn't search very good.

This does what I need:


var text;
try {
// Gecko-based browsers, Safari, Opera.
var serializer = new XMLSerializer();
text = serializer.serializeToString(cell);
} catch (e) {
try {
// Internet Explorer.
text = cell.xml;
}
catch (e) {}
}

return text;