Any reason why this getAttribute call might not work?
============= HTML =================
<html>
<head>
<script>
var xmlDoc;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function init () {
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.addEventListener('load', populateDocument, false);
xmlDoc.load("testXML.xml");
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function populateDocument () {
var nodes = xmlDoc.documentElement.childNodes;
var firstNode = nodes.item(0);
document.getElementById('nodeslength').appendChild(document.createTextNode(nodes.length));
document.getElementById('nodeValue').appendChild(document.createTextNode(firstNode.nodeValue));
document.getElementById('data').appendChild(document.createTextNode(firstNode.data));
document.getElementById('idAttribute').appendChild(document.createTextNode(firstNode.getAttribute("name")));
}
</script>
<title>HTML using XML data</title>
</head>
<html>
<body onload="init();">
<h1>XML DOM Example</h1>
nodes.length: <span id="nodeslength"></span><br>
nodeValue: <span id="nodeValue"></span><br>
node data: <span id="data"></span><br>
id attribute: <span id="idAttribute"></span><br>
</body>
</html>
=================== XML ==================
<?xml version="1.0"?>
<root id="root1" name="rootName">test</root>
================== result ================
XML DOM Example
nodes.length: 1
nodeValue: test
node data: test
id attribute:
---
It never even runs the getAttribute code?
Dave Cline
Dave Cline
==================================
<html>
<head>
<title>HTML using XML data</title>
</head>
<body onLoad="init()">
<h1>XML DOM Example</h1>
xml source: <span id="source"></span><br> <br>
nodes.length: <span id="nodeslength"></span><br>
nodeValue: <span id="nodeValue"></span><br>
node data: <span id="data"></span><br>
id attribute: <span id="idAttribute"></span><br>
name attribute: <span id="nameAttribute"></span><br>
tag Name: <span id="tagName"></span><br>
<br>
<button onclick="loadXMLFromFile()">load XML from file</button>
<button onclick="loadXMLFromTextarea()">load XML from
textarea</button>
<button onclick="clearDocument()">Clear</button><p>
<br>
loadXML source:<br>
<textarea id="taXML" rows="10" cols="30">
<stem id="stem1" name="stemName">flower2</stem>
</textarea>
<script>
var xmlDoc;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function init(){
//--- Add prototypes to Netscape ---
if (!(document.all && document.getElementById)) {
Document.prototype.__defineGetter__("xml", function () { return (new
XMLSerializer()).serializeToString(this);});
Document.prototype.loadXML = function (s) {
// parse the string to a new doc
var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
// remove all initial children
while (this.hasChildNodes())
this.removeChild(this.lastChild);
// insert and import nodes
for (var i = 0; i < doc2.childNodes.length; i++) {
this.appendChild(this.importNode(doc2.childNodes[i], true));
}
};
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function loadXMLFromFile () {
if (document.all && document.getElementById) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async = false;
xmlDoc.load("testXML.xml");
populateDocument();
}
else if (document.implementation &&
document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.addEventListener('load', populateDocument, false);
xmlDoc.load("testXML.xml");
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function loadXMLFromTextarea() {
if (document.all && document.getElementById) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async = false;
xmlDoc.loadXML(document.getElementById("taXML").value);
populateDocument();
}
else if (document.implementation &&
document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.loadXML(document.getElementById("taXML").value);
populateDocument();
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function populateDocument () {
node = xmlDoc.documentElement;
createTextNode('source', xmlDoc.xml);
createTextNode('nodeslength', node.childNodes.length);
createTextNode('nodeValue', node.childNodes.item(0).nodeValue);
createTextNode('data', node.childNodes.item(0).data);
createTextNode('idAttribute', node.getAttribute("id"));
createTextNode('nameAttribute', node.getAttribute("name"));
createTextNode('tagName', node.tagName);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function clearDocument () {
node = xmlDoc.documentElement;
removeTextNodeChildren('source');
removeTextNodeChildren('nodeslength');
removeTextNodeChildren('nodeValue');
removeTextNodeChildren('data');
removeTextNodeChildren('idAttribute');
removeTextNodeChildren('nameAttribute');
removeTextNodeChildren('tagName');
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function createTextNode(docElement, textForNode) {
document.getElementById(docElement).appendChild(document.createTextNode(textForNode));
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function removeTextNodeChildren(docElement) {
var node = document.getElementById(docElement);
while (node.hasChildNodes())
node.removeChild(node.lastChild);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// http://webfx.eae.net/dhtml/xmlextras/xmlextras.html
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// XmlHttp factory
function XmlHttp() {}
XmlHttp.create = function () {
try {
if (window.XMLHttpRequest) {
var req = new XMLHttpRequest();
// some older versions of Moz did not support the readyState
property
// and the onreadystate event so we patch it!
if (req.readyState == null) {
req.readyState = 1;
req.addEventListener("load", function () {
req.readyState = 4;
if (typeof req.onreadystatechange == "function")
req.onreadystatechange();
}, false);
}
return req;
}
if (window.ActiveXObject) {
return new ActiveXObject(getControlPrefix() + ".XmlHttp");
}
}
catch (ex) {}
// fell through
throw new Error("Your browser does not support XmlHttp objects");
};
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getControlPrefix() {
if (getControlPrefix.prefix)
return getControlPrefix.prefix;
var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
var o, o2;
for (var i = 0; i < prefixes.length; i++) {
try {
// try to create the objects
o = new ActiveXObject(prefixes[i] + ".XmlHttp");
o2 = new ActiveXObject(prefixes[i] + ".XmlDom");
return getControlPrefix.prefix = prefixes[i];
}
catch (ex) {};
}
throw new Error("Could not find an installed XML parser");
}
</script>
</body>
</html>
======================== XML file ====