[thelist] XML fragment into DOM

Jeff Howden jeff at jeffhowden.com
Wed Jul 13 22:12:48 CDT 2005


Rod,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: raanders at mailporter.net
> 
> I now have two applications set and ready to use 
> xmlHTTPRequest to pull data/responses from scripts on
> servers.  I'm at the point of getting the data in the
> document.
> 
> Since I write the scripts on the server I can return it
> in any format I want.  I see four different ways for
> one script and three of them for the other.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

Please tell me that you didn't try to write the script for doing the
XMLHTTPRequest yourself.  I say this because there are several very good
libraries out there that already abstract the browser differences for you.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> 1. A full XHTML document that is opened in another
>    window,
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

Doable, but why not just request the XHTML from the server directly rather
than use XHR and populate a popup?

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> 2. a XML fragment that ( hopefully ) could be stuck
>    right into the document at the correct place,
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

This is one of the more common uses.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> 3. an Object or the code to create the object and do an
>    eval on that (?), and lastly
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

I would recommend *against* this approach.  Wherever possible, avoid eval()
like the plague.  There's a reason it sounds so similar to the word evil.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> 4. some custom data format.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

Most often XHR is used to run a "function" on a server, taking the result of
that function and doing something with it in the document making the XHR.
Most often, the datatypes returned by the server are booleans, integers,
floats, and strings.  Once in a while you'll have the need for something
more complex like an array or complex object.  Once you depart from the
simple data types though, the issues become far more complex to work out, no
pun intended.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> My preference is some ( builtin/loadable? ) function to
> turn an xml fragment into part of the DOM.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

If the fragment contains nothing but standard XHTML, inserting that fragment
is as easy as finding the element you wish to make the parent of the
fragment and stuffing it into that element using its innerHTML property.
Purists will gag reading that and suggest that you loop over the structure
of the fragment inserting the various pieces using calls to the
createElement() and appendChild() methods of the document object.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> Suggestions or even better pointers ( my searches --
> five pages into a google search ) didn't help.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

Neuromancer is a *very* robust XHR library.

 [>] Jeff Howden
     jeff at jeffhowden.com
     http://jeffhowden.com/



More information about the thelist mailing list