flatten an array from xml

W

windandwaves

Hi Folk,

I use AJAX to load some XML. When I get back to XML, I want to get a
piece of html that is within <info>... lots of html .... </info>

I want to use:

xmlDoc.getElementsByTagName('info');

but that just returns
[Object HTMLCollection]

How can I retrieve the data within it.

Thank you

Nicolaas
Here is my code:
var i = new Array();
var j = 0;
var jmax = 4;
var http_request = false;
var idname = 'GSmap';

function changemap() {
var variables = getformparameters(document.getElementById('mapform'),
'');
UpdateHtml('_GSmap2.php', 'GSmaptype=8' + variables);
initMapGSmap();
createGSlayer('GSmaptype=1' + variables);
//reset
j = 0;
i = new Array();
return true;
}

function getformparameters(obj, getstr) {
//gets all variables from a form
j++;
for (i[j]=0; i[j] < obj.childNodes.length; i[j]++) {
var newobj = obj.childNodes[i[j]];
tgname = newobj.tagName
if(tgname) {
tgname.toLowerCase;
if (tgname == "INPUT") {
var tvalue = newobj.value;
if(tvalue != 0 && tvalue != "") {
var ttype = newobj.type;
var tname = newobj.name;
if (ttype == "text") {
getstr += "&" + tname + "=" + tvalue ;
}
if (ttype == "checkbox") {
if (newobj.checked) {
getstr += "&" + tname + "=" + tvalue;
}
else {
getstr += "&" + tname + "=0";
}
}
if (ttype == "radio") {
if (newobj.checked) {
getstr += "&" + tname + "=" + tvalue;
}
}
}
}
if (tgname == "SELECT") {
var sel = newobj;
var tvalue = sel.options[sel.selectedIndex].value;
if(tvalue != 0 && tvalue != "") {
getstr += "&" + sel.name + "=" + tvalue;
}
}
}
if(newobj.childNodes.length > 0 && j < jmax) {
getstr = getformparameters(newobj, getstr);
}
}

j--;
return getstr;
}



function UpdateHtml(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
http_request.overrideMimeType('text/xml');
//http_request.overrideMimeType('text/html');
}
}
else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert('could not load data');
}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
var geturl = url + '?' + parameters;
document.getElementById(idname + 'title').innerHTML = "loading new map
.... " + geturl;
http_request.open('GET', geturl, true);
http_request.async = false;
http_request.send(null);
}

function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
var xmlDoc = http_request.responseXML;
var titlearray = xmlDoc.getElementsByTagName('title');
var infoarray = xmlDoc.getElementsByTagName('info');
var zoomarray = xmlDoc.getElementsByTagName('zoom');
var longitudearray = xmlDoc.getElementsByTagName('longitude');
var lattitudearray = xmlDoc.getElementsByTagName('lattitude');
var info = flattenhtmlobject(infoarray, "");
document.getElementById(idname + 'title').innerHTML =
titlearray[0].firstChild.nodeValue;
document.getElementById(idname + 'info').innerHTML = info;
alert(zoomarray[0].firstChild.nodeValue);
alert(longitudearray[0].firstChild.nodeValue);
alert(lattitudearray[0].firstChild.nodeValue);
}
else {
alert('There was a problem with the request.');
}
}
else {
}
}

function flattenhtmlobject(obj, output) {
output = titlearray[0].firstChild.nodeValue;
return output;
}
 
L

Laurent Bugnion

Hi,
Hi Folk,

I use AJAX to load some XML. When I get back to XML, I want to get a
piece of html that is within <info>... lots of html .... </info>

I want to use:

xmlDoc.getElementsByTagName('info');

but that just returns
[Object HTMLCollection]

getElementsByTagName returns a collection of Nodes. Collections in
JavaScript can be handled like arrays, and indexed.

If you're sure that you have only one node named "info", you can use

var nInfo = xmlDoc.getElementsByTagName( "info" )[0]

which is a Node expression.

After that, you can use

nInfo.firstChild.nodeValue

for example, to access the content of the text node which is the child
of info.

HTH
Laurent
 
W

windandwaves

Laurent said:
Hi,
Hi Folk,

I use AJAX to load some XML. When I get back to XML, I want to get a
piece of html that is within <info>... lots of html .... </info>

I want to use:

xmlDoc.getElementsByTagName('info');

but that just returns
[Object HTMLCollection]

getElementsByTagName returns a collection of Nodes. Collections in
JavaScript can be handled like arrays, and indexed.

If you're sure that you have only one node named "info", you can use

var nInfo = xmlDoc.getElementsByTagName( "info" )[0]

which is a Node expression.

After that, you can use

nInfo.firstChild.nodeValue

for example, to access the content of the text node which is the child
of info.


Thank you Laurent for your help... Cool. This s what I found worked,
with some help from another person:


function serializeNode(node) {
if(node != undefined) {
var xml = "";
if(_browser.isSafari) {
xml = xmlText(node);
}
else if(_browser.isIE) {
xml = node.xml;
}
else {
var serializer = new XMLSerializer();
xml = serializer.serializeToString(node);
}
return xml;
}
else {
return undefined;
}
}

var DOM_ELEMENT_NODE = 1;
var DOM_TEXT_NODE = 3;

function xmlText(node) {
var ret = '';
if (node.nodeType == DOM_TEXT_NODE) {
ret += node.nodeValue;
}
else if (node.nodeType == DOM_ELEMENT_NODE) {
ret += '<' + node.nodeName;
for (var i = 0; i < node.attributes.length; ++i) {
var a = node.attributes;
if (a && a.nodeName && a.nodeValue) {
ret += ' ' + a.nodeName;
ret += '="' + a.nodeValue + '"';
}
}
if (node.childNodes.length == 0) {
ret += '/>';
}
else {
ret += '>';
for (var i = 0; i < node.childNodes.length; ++i) {
ret += arguments.callee(node.childNodes);
}
ret += '</' + node.nodeName + '>';
}
}
return ret;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top