Hi I'm working on using an example out of the book on retrieving XML and I'm starting to get really turned around in making it work for Mozilla. I am also wondering if I could use the oXmlDom.getElementByTagName() method but for some reason when I do that I get an error. It tells me that oXmlDom has no properties. I'm new to all the XML and javascript world so I'm not sure what I'm doing wrong. But here is a current code example of what I'm working on:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Cross-Browser XML DOM Example</title>
<script type="text/javascript" src="scripts/detect.js"></script>
<script type="text/javascript" src="scripts/xmldom.js"></script>
<style type="text/css">
body {
font-family: "Trebuchet MS", Arial, Helvetica;
font-size: 10pt;
}
.msg {
width: 500px;
font-family:"Trebuchet MS", Arial, Helvetica;
border: 3px solid #ccc;
padding: 10px;
}
html>body .msg {
width: 490;
}
.line {
border-bottom: 2px solid grey;
width: 600px;
}
.codeMsg {
font-style: italic;
font-weight: bold;
color: #CC3366;
}
</style>
</head>
<body>
<p>
This example loads a valid XML file and displays an alert showing us what
the root element is and how many child nodes there are.
</p>
<script type="text/javascript">
var oXmlDom = new XmlDom();
oXmlDom.onreadystatechange = function () {
if (oXmlDom.readyState == 4) {
if (oXmlDom.parseError != 0) {
var oError = oXmlDom.parseError;
alert("An error occurred:\nError Code: "
+ oError.errorCode + "\n"
+ "Line: " + oError.line + "\n"
+ "Line Pos: " + oError.linepos + "\n"
+ "Reason: " + oError.reason);
}
}
};
// Load the xml
var file = "xml/e_learning.xml";
oXmlDom.load(file);
</script>
<h3>Message that XML document has loaded</h3>
<div class="msg">
<script type="text/javascript" language="javascript">
if(isMoz)
{
oXmlDom.onload = document.write('The XML file: <span class=\"codeMsg\">'+file+'</span> has been loaded');
}
</script>
</div>
<h3>We are testing the existance of: <span class="codeMsg">oXmlDom</span></h3>
<div class="msg">
<div class="codeMsg">
<script type="text/javascript" language="javascript">
document.write(oXmlDom);
</script>
</div>
<br />
<script type="text/javascript" language="javascript">
var oEvaluator = new XPathEvaluator();
var oResult = oEvaluator.evaluate("welcome/title", oXmlDom.documentElement, null, XPathResult.STRING_TYPE, null);
//alert(oResult.stringValue);
document.write(oResult.stringValue);
</script>
</div>
</body>
</html>
My xml document is (e_learning.xml) in the folder "xml":
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<course>
<welcome>
<title>C17 FEDS</title>
</welcome>
</course>
I'm trying to get to the title...so how do I do this?
When running this code i get this error
Code:
Error: uncaught exception: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIDOMXPathEvaluator.evaluate]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: file:///Macintosh%20HD/Documents/Business/Intrepid/Projects/excel_to_xml/CrossBrowserXmlDomExample2.html :: <TOP_LEVEL> :: line 82" data: no]
What am I doing wrong?