Uhmm... I guess I forgot to mention how to eventually get XML from
responseText for IE. IE's serialization ways are numerous, version-
dependent and boring. See for instance:
http://groups.google.com/group/microsoft.public.xml/browse_frm/thread....
Because for other browsers this problem doesn't arise, we can be IE
specific and use quick, dirty and amusingly effective way using IE-
specific XML Data Islands tools and IE's <XML> tag.
So assuming we've got responseText filled, responseXML empty and we
know that it should be a well-formed XML file, we simply do that:
var tmp = document.createElement('DIV');
tmp.innerHTML = '<XML>' + xhr.responseText + '</XML>';
return tmp.firstChild.XMLDocument.documentElement;
Respectively for a HTML fragment we can do:
var tmp = document.createElement('DIV');
tmp.innerHTML = xhr.responseText;
return tmp.firstChild;
Enjoy!
I guess now it should be finally answered to the foka's OP that
started this:
http://groups.google.com/group/comp.lang.javascript/msg/08d156643e084fba
I'll post a copy to the relevant thread as well
1) OP path "../xml/groups.xml" is not universally possible because Fx
allows to read only in the same directory and subdirectories, but not
in top or sibling directories. So the directory structure has to be
arganized accordingly.
2) Let's have XML data in groups.xml file like this:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<group>AK</group>
<name>COS AK</name>
</item>
<item>
<group>BZ</group>
<name>COS BZ</name>
</item>
<item>
<group>CA</group>
<name>COS CA</name>
</item>
</items>
3) Let's have a HTML page with the script in the same directory where
groups.xml is. Then to read XML data and to populate select form
element on the page the script could be like this (tested IE6, IE8,
Fx, Sf, Ch, Op):
<!DOCTYPE html>
<html>
<head>
<title>XMLHttpRequest local XML file reading test</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function getAjaxObject(forLocalData) {
var f = arguments.callee;
var isIE = /*@cc_on true || @*/ false;
var xhr = null;
if (typeof window == 'object') {
/* Common branch for modern browsers */
if (
(typeof window.XMLHttpRequest != 'undefined') &&
/* window.XMLHttpRequest instances in IE do not
* have local file access even from local pages,
* unlike ActiveX instances: so if forLocalData
* flag set, we are trying to use the underlaying
* ActiveX constructor.
*/
( !(isIE && forLocalData) )
) {
try {
return new window.XMLHttpRequest();
}
catch(e) {
return new Error(e.message);
}
}
/* ActiveX branch for old IE versions and
* for local data access capable instances. See:
* 1)
http://tinyurl.com/MSDNBlog1
* 2)
http://tinyurl.com/MSNewsGroup1
* 3)
http://tinyurl.com/MSDNBlog2
* for proper ProgID and proper ProgID trying sequence.
*
* Correction from the test lab: contrary to what MS says
* and in support of my arguments at 2) discission a set
* of machines with IE6 / Win XP SP2 in default installation
* failed to puck up on Msxml2.XMLHTTP.3.0 so used the older
* version: yet they happily picked up on Msxml2.XMLHTTP.4.0
* Whatever the exact reasons of it are, a branch for 4.0
* is added to ensure the newest version usage in all
* given circumstances.
*/
else if (
(isIE) &&
/* if IE 5.x for MacOS without ActiveX: */
(typeof window.ActiveXObject != 'undefined')
) {
try {
xhr = new window.ActiveXObject('Msxml2.XMLHTTP.6.0');
f.isActiveX = true;
f.ActiveXVersion = 6;
return xhr;
}
catch(e) {
try {
xhr = new window.ActiveXObject('Msxml2.XMLHTTP.3.0');
/* Contrary to numerous MS claims like here:
*
http://tinyurl.com/SelectionLanguage
* Msxml2.XMLHTTP.3.0 (IE6 / Win XP SP2 by default)
* does not have XSL Patterns as the default language
* neither it supports setProperty method so trying
* to apply setProperty("SelectionLanguage", "XPath")
* leads to "not supported" runtime error on IE6.
* In the reality the last COM with XSL Patterns were
* in XMLHTTP.2.x versions. Looks pretty much as a MS
* internal urban legend.
*/
f.isActiveX = true;
f.ActiveXVersion = 3;
return xhr;
}
catch(e) {
/* As per MS Security Bulletin
http://tinyurl.com/MS06-061
* Msxml2.XMLHTTP.2.x and older like Microsoft.XMLHTTP can
* be blocked and overall the security considerations
* should prevail over the maximum backward compatibility.
* Add these branches only if you really need to support
* IE 5.x in a non-commercial home use solution.
*/
return new Error(e.message);
}
}
}
/* If nothing then nothing... */
else {
return new Error('ActiveX is missing or blocked');
}
}
else {
return new Error('window host object is missing');
}
}
window.onload = function() {
document.forms[0].reset();
window.setTimeout('test()', 10);
}
function test() {
var isIE = /*@cc_on true || @*/ false;
var wantXML = true;
var xmlDoc = null; var tmp = null;
if ((xhr = getAjaxObject(true)) instanceof Error) {
window.alert('Couldn\'t instantiate XHR:\n' + xhr.message);
return;
}
if (loadData('groups.xml') instanceof Error) {
window.alert('Couldn\'t read data:\n' + xhr.message);
return;
}
if (isIE && wantXML) {
tmp = document.createElement('DIV');
tmp.innerHTML = '<XML>'+xhr.responseText+'</XML>';
xmlDoc = tmp.firstChild.XMLDocument.documentElement;
}
else {
xmlDoc = xhr.responseXML;
}
var out = document.forms[0].groups;
var groupXml = xmlDoc.getElementsByTagName('group');
for (var i=0; i<groupXml.length; i++) {
out.options
= new Option(groupXml.firstChild.nodeValue,
groupXml.firstChild.nodeValue);
}
function loadData(path, wantXML) {
try {
xhr.open('GET', path, false);
xhr.send('');
}
catch(e) {
return new Error(e.message);
}
}
}
</script>
</head>
<body>
<h1>XMLHttpRequest local XML file reading test</h1>
<form action="" onsubmit="return false;">
<fieldset>
<select name="groups">
</select>
</fieldset>
</form>
</body>
</html>