473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Copy responseXML DOM fragment to the document

I have an http = new XMLHttpRequest(); that provides me with an
http.responseXML. Somewhere deep in the http.responseXML there is a fragment
called e.g. mydom that I want to copy with all its children to the document.

document.getElementsByTagName('body')[0].innerHTML=mydom; does not work. What
should I do? Traverse mydom manually? Look for a JS library that does this
already?

--
Alexander Mikhailian

Oct 15 '05 #1
3 8548


Alexander Mikhailian wrote:
Somewhere deep in the http.responseXML there is a fragment
called e.g. mydom that I want to copy with all its children to the document.

document.getElementsByTagName('body')[0].innerHTML=mydom; does not work. What
should I do?


Well
document.getElementsByTagName('body')[0].innerHTML
suggests you have script in an HTML document so to have any chance to
simply move nodes from that responseXML document to the HTML document
those nodes should be XHTML nodes then.
And the XML DOM and the HTML DOM must be a common DOM which allows to
use importNode to to import nodes from one document to another, like
Mozilla or Opera do.
So with Mozilla and with Opera you could have for instance the XML

<?xml version="1.0" encoding="UTF-8"?>
<data>
<div xmlns="http://www.w3.org/1999/xhtml">
<p>Kibology for all.</p>
<p>All for Kibology.</p>
</div>
</data>

and then you could do e.g.

var responseXML = httpRequest.responseXML;
var div;
if (responseXML && typeof responseXML.getElementsByTagNameNS !=
'undefined' && (div =
responseXML.getElementsByTagNameNS('http://www.w3.org/1999/xhtml',
'div')[0])) {
var clonedDiv = document.importNode(div, true);
document.body.appendChild(clonedDiv);

to import that XHTML <div> element and its contents into the HTML document.
But within IE/Win you use the HTML DOM MSHTML implements and the XML DOM
MSXML implements and those are completely separate implementations where
you can't simply move/import nodes from one document to the other. Nor
does MSXML in any way recognize the XHTML namespace and build XHTML
nodes when parsing that XML. Thus with IE you need to either write your
own code traversing the XML DOM and creating HTML nodes as needed or you
need to try to parse the XML as HTML tag soup in IE using innerHTML or
insertAdjacentHTML. So there you could try
htmlElement.innerHTML = xmlNode.xml

Or you could use XSLT first to transform the XML to HTML as a string and
then use innerHTML or insertAjdacentHTML.

So going cross browser gets complicated but libraries are around that
could ease your task, at least initially, until you run into problems as
a library might favour a certain approach or implementation and then
fails to mimic that approach correctly with other implementations.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 16 '05 #2
Martin Honnen <ma*******@yahoo.de> wrote:
Alexander Mikhailian wrote:
Somewhere deep in the http.responseXML there is a fragment
called e.g. mydom that I want to copy with all its children to the document.

So going cross browser gets complicated but libraries are around that
could ease your task, at least initially


Thank you for such a complete explanation of the subject! One question
remains, though: wouldn't it be wiser to ignore the importNode (as a DOM level
2 function) and innerHTML (as a hack) and use a library that copies one DOM to
another (copyDOMs(DOM1, DOM2)?) by walking over the source DOM using just DOM
level 1 functions that are supposedly more or less the same among different
browsers?

Given that DOM traversal is an intricate subject in itself, I wonder if there
a is a stable implementation already.

--
Alexander Mikhailian

Oct 16 '05 #3


Alexander Mikhailian wrote:
wouldn't it be wiser to ignore the importNode (as a DOM level
2 function)
Well if you already have HTML nodes and simply need to import them into
another document then with those implementations that support that you
certainly gain stability and performance compared to any attempts to
write that yourself or to serialize first and then reparse.
and innerHTML (as a hack) and use a library that copies one DOM to
another (copyDOMs(DOM1, DOM2)?) by walking over the source DOM using just DOM
level 1 functions that are supposedly more or less the same among different
browsers?


I think the Sarissa library for instance for some time tried to
implement importNode for IE by walking one DOM and creating nodes in the
other DOM but found setting someDiv.innerHTML =
someResponseMarkupAsString to be much faster.

As for DOM Level 1 and sticking with that, I am not sure that helps
much, if you want to process XML with namespaces (XHTML for instance,
Atom feeds, SVG, to name a few) then you need DOM Level 2 stuff to
correctly deal with namespaces (or you need to go XPath as with MSXML in
IE).
And then when it comes to create stuff in an HTML document with the DOM
you can have bad suprises if you try DOM Level 1 Core stuff like
setAttribute where IE has its own legacy implementation for instance not
being compatible with the W3C DOM specification (mainly by not
distinguishing between the attribute in the core DOM and the property in
the HTML DOM).
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Chris Smith | last post by:
Been banging my head against this one for some time. I'm attempting to use XmlHTTPRequest to read an XML document from the web server and interact with it using the DOM. So far, I've had less...
2
by: Marco Laponder | last post by:
Hi All, I am using XmlHttpRequest object within Firefox to get a xml document from the servlet. The reponseText is set but the responseXml is null. My Code is: req = new XMLHttpRequest();...
4
by: Sanjay Dahiya | last post by:
I tried POSTing from XMLHttpRequest, i can get the XML right on server but responseXML from server is coming null. I can see the XML right in responseText. but responseXML is null. responseText to...
4
by: Anand | last post by:
The situation is; I receive a response back from the server which i only a part of html code. e.g. a table like <table <tr><td>high</td></tr>........ </table>. In other words, I receive file as a...
3
by: Gustaf | last post by:
I'm trying to grasp this little passage from the XBRL spec: "The xlink:href attribute MUST be a URI. The URI MUST point to an XML document or to one or more XML fragments within an XML document....
1
by: MD | last post by:
Following snippet is the portion of my program to get the location list in xml with AJAX when i alert(request.responseText) it manages to display the xml document like the following <? xml...
14
by: webEater | last post by:
I downloaded IE7 and tried out the native XMLHttpRequest support. I think there is an object in IE7 called "XMLHttpRequest" but I ask me why it is called "XML"HttpRequest. I made a request to an...
1
by: Sand Yaah | last post by:
i went thru a discussion put by eros and helped out by dmjpros. d questions asked were right and i tried each but there was no problem there. my code returns null in xmlHttp.responseXML and...
6
by: KDawg44 | last post by:
Hi, My responseXML is always null on my AJAX call. When I browse directly to the PHP script I am calling, the XML file shows up just fine. I have read that if a returned XML file is not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.