Basscyst
03-09-2007, 01:06 AM
In the past whenever I needed to get a bit of data from the server through an xmlHTTPRequest, I could do one of two things:
Serve my self up the data in xml and then parse through each xml tag and create an appropriate html tag. In turn appending any applicable data to each html element. -- Big hassle but imho the right way.
The other way (the dirty way) to just get the responseText and change the innerHTML of my target element. -- Nice and easy but dirty.
Now I thought, it sure would be nice if there was a responseXHTML that I could traverse to get the top node of the xhtml that I am interested in and simply append that to my target dom node.
There is no such thing that I am aware of, but I've come up with the following that attempts to do just that. It all seems to be working for the most part, however, it's appending an undefined element to the target node, it is unaparent when rendered in the browser window, but in the DOM inspector it is viewable. I'd also appreciate any feedback on things I may not have taken into consideration.
Below is the code that parses the XML:
function toXHTML(obj){
var div=document.createElement("div");
makeChild(obj,div);
function makeChild(src_xml,targ_html){
var new_html=transformXML(src_xml);
targ_html.appendChild(new_html);
var kids=src_xml.childNodes;
var len=kids.length;
for(var i=0;i<len;i++){
makeChild(kids[i],new_html);
}
}
return div;
}
function transformXML(obj){
if(obj.tagName){
var ie = (document.all && !window.opera)?1:0;
var flg=0;
if(ie){
if(obj.tagName.toLowerCase()=="input"){
if(obj.getAttribute('type').toLowerCase()=="radio"||obj.getAttribute('type').toLowerCase()=="checkbox"){
var flg=1;
var html_obj=document.createElement('<'+obj.tagName+' name='+obj.getAttribute("name")+'" />');
}else{
var html_obj=document.createElement(obj.tagName);
}
}else{
var html_obj=document.createElement(obj.tagName);
}
}else{
var html_obj=document.createElement(obj.tagName);
}
var atts=obj.attributes;
var len=atts.length;
for(var i=0;i<len;i++){
switch(atts[i].name.toLowerCase()){
case "name":
if(flg==0){
html_obj.name=atts[i].value;
}
break;
case "class":
html_obj.className=atts[i].value;
break;
case "value":
html_obj.value=atts[i].value;
break;
case "colspan":
html_obj.setAttribute("colSpan",atts[i].value);
break;
default:
html_obj.setAttribute(atts[i].name.toLowerCase(),atts[i].value);
}
}
}else{
html_obj=document.createTextNode(obj.nodeValue);
}
return html_obj;
}
Please note if you try to use this, it isn't expecting event handlers in your tags, event handlers can be added from script on the calling page.
Serve my self up the data in xml and then parse through each xml tag and create an appropriate html tag. In turn appending any applicable data to each html element. -- Big hassle but imho the right way.
The other way (the dirty way) to just get the responseText and change the innerHTML of my target element. -- Nice and easy but dirty.
Now I thought, it sure would be nice if there was a responseXHTML that I could traverse to get the top node of the xhtml that I am interested in and simply append that to my target dom node.
There is no such thing that I am aware of, but I've come up with the following that attempts to do just that. It all seems to be working for the most part, however, it's appending an undefined element to the target node, it is unaparent when rendered in the browser window, but in the DOM inspector it is viewable. I'd also appreciate any feedback on things I may not have taken into consideration.
Below is the code that parses the XML:
function toXHTML(obj){
var div=document.createElement("div");
makeChild(obj,div);
function makeChild(src_xml,targ_html){
var new_html=transformXML(src_xml);
targ_html.appendChild(new_html);
var kids=src_xml.childNodes;
var len=kids.length;
for(var i=0;i<len;i++){
makeChild(kids[i],new_html);
}
}
return div;
}
function transformXML(obj){
if(obj.tagName){
var ie = (document.all && !window.opera)?1:0;
var flg=0;
if(ie){
if(obj.tagName.toLowerCase()=="input"){
if(obj.getAttribute('type').toLowerCase()=="radio"||obj.getAttribute('type').toLowerCase()=="checkbox"){
var flg=1;
var html_obj=document.createElement('<'+obj.tagName+' name='+obj.getAttribute("name")+'" />');
}else{
var html_obj=document.createElement(obj.tagName);
}
}else{
var html_obj=document.createElement(obj.tagName);
}
}else{
var html_obj=document.createElement(obj.tagName);
}
var atts=obj.attributes;
var len=atts.length;
for(var i=0;i<len;i++){
switch(atts[i].name.toLowerCase()){
case "name":
if(flg==0){
html_obj.name=atts[i].value;
}
break;
case "class":
html_obj.className=atts[i].value;
break;
case "value":
html_obj.value=atts[i].value;
break;
case "colspan":
html_obj.setAttribute("colSpan",atts[i].value);
break;
default:
html_obj.setAttribute(atts[i].name.toLowerCase(),atts[i].value);
}
}
}else{
html_obj=document.createTextNode(obj.nodeValue);
}
return html_obj;
}
Please note if you try to use this, it isn't expecting event handlers in your tags, event handlers can be added from script on the calling page.