When building an ajax application, I normally prefer fetching remote data in JSON format. However, when the hierarchy is unpredictable (specifically geocoding responses from Google Maps API), it is tempting to go with XML format because the DOM method getElementsByTagName will get the data wherever it is in the hierarchy. Assuming there is only one element/property with the tag/name I'm interested in, is the following function an efficient equivalent to getElementsByTagName? How can it be improved?
function findProperty(obj,prop){
for(var p in obj){
if(p==prop){
return obj[p];
}
if(obj[p] instanceof Object){
var tmp = findProperty(obj[p],prop);
if(tmp){
return tmp;
}
}
}
}