/    Sign up×
Community /Pin to ProfileBookmark

Cross Browser loading and handling XML documents without XMLHttp request

hi

how can i load and handle XML documents without XMLHttp Request in javascript that support famous browsers like:

FireFox2, 3, IE 6, 7, opera9+, Safari

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@felgallAug 17.2008 — The same way you would fly to the moon without using a spacecraft.

If you don't want to use the only way provided for reading XML into JavaScript then you are not going to be able to read XML into JavaScript.
Copy linkTweet thisAlerts:
@itgate_irauthorAug 17.2008 — i mean with command like:

[CODE] oXmlDom = document.implementation.createDocument("","",null);
oXmlDom.load("something.xml");[/CODE]

or

[CODE]
function createDocument() {
var aVersions = [ "MSXML2.DOMDocument.5.0",
"MSXML2.DOMDocument.4.0","MSXML2.DOMDocument.3.0",
"MSXML2.DOMDocument","Microsoft.XmlDom"
];

for (var i = 0; i < aVersions.length; i++) {
try {
var oXmlDom = new ActiveXObject(aVersions[i]);
return oXmlDom;
} catch (oError) {
//Do nothing
}
}
throw new Error("MSXML is not installed.");
}
[/CODE]
Copy linkTweet thisAlerts:
@A1ien51Aug 18.2008 — I think you ansered your own question....I am not sure why you would use that over the XMLHttpRequest object. Guessing you get a null error and think another way will fix it OR you have requirements that are very strict and not smart. ?

Eric
Copy linkTweet thisAlerts:
@itgate_irauthorAug 18.2008 — you are right

i mean working with xml,xpath, xslt

i write a code for XML

it works in IE and Firefox and not in opera!

i don't know how to deal with xpath , xslt in javascript ??
Copy linkTweet thisAlerts:
@slaughtersAug 18.2008 — How does "new ActiveXObject(aVersions[i]);" work in Firefox ?
Copy linkTweet thisAlerts:
@itgate_irauthorAug 19.2008 — it is only for IE not firefox

buy i don't know how to handle xsl, xml with them?
Copy linkTweet thisAlerts:
@slaughtersAug 19.2008 — Then your statements are kind of confusing and your posts title a little mis-leading.

I wish you luck.
Copy linkTweet thisAlerts:
@Angry_Black_ManAug 20.2008 — In communist russia, XML documents handle you!

How does "new ActiveXObject(aVersions[i]);" work in Firefox ?[/QUOTE]



truth.
Copy linkTweet thisAlerts:
@rnd_meAug 20.2008 — it is only for IE not firefox

buy i don't know how to handle xsl, xml with them?[/QUOTE]


you are in luck.

i found a simple example i worked out a couple years back.

just tested it in ff3 and ie7, still works.

it simply transforms the two files and displays the output.

this demo use a xsl template named "combo.xsl".

it is meant to transform an rss feed into a drop-down box.


combo.xsl:
[CODE]<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html"/>

<xsl:template match="/rss/channel">

<body style="background-color: black; margin: 0px">
<select onchange='window.open(this.value, "")'>

<xsl:apply-templates select="item"/>

</select>
</body>
</xsl:template>

<xsl:template match="/rss/channel/item">

<option value="{link}"><xsl:value-of select="title" /></option>

</xsl:template>

</xsl:stylesheet>[/CODE]








the xsldemo.html page:

[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>dan's client-side xsl demo</client> </title>
</head>
<body onLoad="runTransform('combo.xsl', 'you can change this to another xsl file path');" >

<div id="dump">
You need to : <br />
1. pass the right xslt file path to the runTransform funtion. <br />
2. put the url of the xml document after a ? in the page url: the 'querystring'
</div>

<script type="text/javascript">
function runTransform(ssUrl ){
var turl = window.location.search.substr(1); //load the data document url from the queryString

if(document.implementation && document.implementation.createDocument){
// Mozilla

var xsltProcessor = new XSLTProcessor();

// load the xslt file
var x= new XMLHttpRequest();
x.open("GET", ssUrl , false);
x.send(null);

// get the XML document
xslStylesheet = x.responseXML;
xsltProcessor.importStylesheet(xslStylesheet);

// load the xml file
x= new XMLHttpRequest();
x.open("GET", turl, false);
x.send(null);

var xmlSource = x.responseXML;

//transform
var resultDocument = xsltProcessor.transformToFragment(xmlSource, document);
document.getElementById("dump").innerHTML= "";
document.getElementById("dump").appendChild(resultDocument);

}else if(window.ActiveXObject){
// IE

// Load XML
xml = new ActiveXObject("MSXML2.DOMDocument");
xml.async = false

xml.load(turl)

// Load XSL
xsl = new ActiveXObject("MSXML2.DOMDocument");
xsl.async = false
xsl.load(ssUrl )


// Transform document.getElementById("dump").innerHTML=xml.transformNode(xsl);
}else{
alert("XSLT not available...");
}

}

</script>
</body>
</html>
[/CODE]


you will have to provide the rss file to use, and put its (same-domain) url in the querystring when viewing the html page.
Copy linkTweet thisAlerts:
@slaughtersAug 20.2008 — Ahhh...

That jogged my memory. Here is an old Quirksmode article on importing XML documents:

http://www.quirksmode.org/dom/importxml.html

Should work in IE, Mozilla (Netscape), Opera, and Firefox.

For Safari support you'll need to use XMLHttpRequest :

http://developer.apple.com/internet/webcontent/xmlhttpreq.html

(which should also work for IE, Mozilla (Netscape), Opera, and Firefox ? )
Copy linkTweet thisAlerts:
@HoboScriptAug 20.2008 — Question... why are you trying to apply xslt, xml and xpath in javascript? Is this for some kind of PoC thing?

Just curious.
Copy linkTweet thisAlerts:
@rnd_meAug 20.2008 — Ahhh...

That jogged my memory.[/QUOTE]

lol-

tell me about it.

its like: what ever happened to xslt?

hell, whatever happened to xml in general?

where is soap and wap and rfd? all i see is rss...

i though this stuff was 'the next big thing' around 2006.
×

Success!

Help @itgate_ir spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...