views:

242

answers:

2

Greetings. Here is an XML object returned by my server in the responseXML object:

<tableRoot>
   <table>
      <caption>howdy!</caption>
      <tr>
         <td>hello</td>
         <td>world</td>
      </tr>
      <tr>
         <td>another</td>
         <td>line</td>
      </tr>
   </table>

Now I attach this fragment to my document tree like so:

getElementById('entryPoint').appendChild(responseXML.firstChild.firstChild);

But instead of being rendered as a table, I get the following text:

howdy! helloworldanotherline

The same result occurs of I replace firstChild.firstChild with just firstChild. It seems like I'm just getting the nodeValues, and all of the tags are stripped out?! Am I fundamentally misunderstanding what the responseXML object is supposed to represent? This works, BTW, if I take out the 'root' tags, and set innerHTML to responseText.
Can someone please enlighten me on the correct way to use responseXML?

+1  A: 

You can create an element at the position you want to insert and than do

element.innerHTML = request.responseText

Norbert Hartl
Thank you, and I have gotten that approach to work. But I feel there would be serveral advantages to using appendChild. For one thing, I could then later use replaceChild to swap content back and forth. And with innerHTML I can't as easily loop through the tree deleting callback references or changing attributes. At any rate, I simply do not understand the responseXML object. It does not seem to act the same way as the objects I build from scratch using appendChild. Either I'm thinking about this wrongly, or I have an implementation error. At the moment, I don't know which it is!
I don't understand really. After doing innerHTML everything is converted to DOM nodes on which you can do anything you need. Nothing magical about it.
Norbert Hartl
Ok, thanks. I was obviously confused. So innerHTML attaches my table to the tree, while using the DOM methods to attach responseXML does nothing useful. Kind of surprising.
I don't know the exact reasons but we avoid using responseXML, too
Norbert Hartl
Did you consider rating and/or accepting the answer? :)
Norbert Hartl
+2  A: 

You get the text instead of a table, because you use pure DOM for manipulations and your response XML doesn't have the namespaces declarations. So when appending an XML element browser doesn't know whether your "table" tag is from HTML, XUL, SVG or else from.

1) Add namespace declaration:

<table xmlns="http://www.w3.org/1999/xhtml"&gt;

2) Instead of directly inserting a reffered XML DOM Element, you should first import that node into your HTML DOM Document:

var element = document.importNode(responseXML.firstChild.firstChild, true);
document.getElementById('entryPoint').appendChild(element);

Hope this helps!

Sergey Ilinsky