![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
XML DOM in JS - Extracting everything from an element
'Lo there all. I've started trying to fiddle with AJAX, and I've run into a bit of a problem.
I'm trying to use asynchronous HTTP requests to load XML pages through the XMLHttpRequest object in JavaScript. The thing is, my pages contain a lot of tags, attributes, etc. and the only code I've found for importing data into a page once it's been retrieved deals with plain text (using the responseText property or through responseXML.getElementByTag("tag").items(n).value). I can't find information on loading everything under the document root into a string, let alone running it through an XSL parser to turn it into HTML. Here's an example of what the XML looks like: <?php include 'xmlheader.php'; ?> <main> <section> <heading>Home</heading> <p>Welcome to my site.</p> <p>Click on the links to the left and right to browse around.</p> </section> <section> <heading>News</heading> <news> <date>2005-01-28</date> <time>14:42:00</time> <author>Ooble</author> <headline>Headline</headline> <content> <p>Blah blah blah.</p> </content> </news> </section> <sidebar> <heading>Random</heading> <list type="none"> <item><link url="/news/newsitem1/">News Item #1</link></item> <item><link url="/news/newsitem2/">News Item #2</link></item> <item><link url="/news/newsitem3/">News Item #3</link></item> </list> </sidebar> </main> I need to turn that into HTML using my XSL stylesheet, and put everything excluding the root node into a <div> tag. Any ideas? |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 122
Rep Power: 3
![]() |
Why do you need JS?
__________________
"I'm going to become rich and famous when I invent a device that allows you to stab people in the face over the internet" |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 122
Rep Power: 3
![]() |
XML is over-rated anyway, IMO.
You might consider looking into JSON for passing data from the server to the client; I find it slightly easier to deal with... http://www.json.org/ http://www.json.org/js.html --47.
__________________
"I'm going to become rich and famous when I invent a device that allows you to stab people in the face over the internet" |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I'm using XML because I feel like it, to be honest. I've got this far - I even bought a book on XSLT - and I'm not backing down now. I will check out JSON though, but not for this project.
![]() I need JavaScript for asynchronous loading - I don't want to have to load a new page. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 122
Rep Power: 3
![]() |
Ok then, you can get the whole lot into a string with something
simple like: var str=xmlFile.getElementsByTagName('name')[0].nodeVlaue;http://www.quirksmode.org/dom/importxml.html
__________________
"I'm going to become rich and famous when I invent a device that allows you to stab people in the face over the internet" |
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I swear I tried that... gimme a sec.
![]() EDIT: Yup. Returns NULL. |
|
|
|
|
|
#7 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
XMLHttpRequest returns an XMLDocument.
You will use XMLHttpRequest to load the XSL and the content. The XSL is typically loaded when the page loads. Whenever you happen to load the content, you can use XSLTProcessor.transformToDocumentFragment(). A DocumentFragment is a node, so can be added to your <div> with appendChild. http://developer.mozilla.org/en/docs...nt_Information http://www.xulplanet.com/references/...LDocument.html http://www.xulplanet.com/references/...Processor.html http://www.soi.city.ac.uk/~sa386/epterm2/sqlxml/week9/The%20XSLT-JavaScript%20Interface%20In%20Gecko.htm For some unknown reason, neither Firefox nor IE lets you see the result of XSL transforms, so I highly reccomend XMLStarlet for debugging. Result tree, errors, etc. You can even validate against the W3C DTDs.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#8 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
I put together a sample out of boredom.
Doesn't work in IE for one reason or another, but you get the idea. Requires a server (local or otherwise) to operate. Clickity
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#9 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Cheers - that's exactly what I'm looking for (I think). I'll play with it in a bit.
![]() |
|
|
|
|
|
#10 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
Possibly the problem with IE is that it requires that the object be specified differently. I use something like: if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest ();
xmlhttp.onreadystatechange = xmlhttpChange;
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = xmlhttpChange;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|