Click to See Complete Forum and Search --> : Help: Using XMLDOM


markem
05-12-2006, 01:02 PM
First off, I apologize to those who will recognize this post from the JavaScript forum. I'm posting it here because there haven't been any responses there. And I do need the help.

This is my first experience with XML. I've been reading through the XML DOM section on this website and have been trying to use the examples provided, but I keep getting errors trying to step through (even just read) the data. I've tried consolidating the methods (using xmlDoc.async="false") and I still get errors. Scenario: I'm trying to provide a way to import an XML file (formatted for Excel) from the corporate intranet, place the data into existing HTML input elements for quality control, then save the data to a database. My JS knowledge is just enough to be dangerous - in other words I'm no expert. I would GREATLY appreciate any help!

My testing data file in its entirety contains:

<Employee>
<ID>6880</ID>
</Employee>

Here is my code:

function importXML(fieldName)
{
fileName = document.forms[0].elements[fieldName].value;

xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) parseXML()
};
xmlDoc.loadXML(fileName);
}

function parseXML()
{
document.write("<br>Error Code: ");
document.write(xmlDoc.parseError.errorCode);
document.write("<br>Error Reason: ");
document.write(xmlDoc.parseError.reason);
document.write("<br>Error Line: ");
document.write(xmlDoc.parseError.line);

var doc=xmlDoc.documentElement;
var x = xmlDoc.getElementsByTagName('Employee');
}

</script>

<form method="POST"
enctype="multipart/form-data"
action="saveFile.asp">
<input type="file" name="fileName" size="60">
<input type="submit" VALUE="Load File" onClick="importXML('fileName')">
</form>

Script Error: "Object required"

Output:

Error Code: -1072896682
Error Reason: Invalid at the top level of the document.
Error Line: 1

Ultimater
05-15-2006, 08:12 AM
Edit your XML document like so:

<documentroot>
<Employee>
<ID>6879</ID>
</Employee>
<Employee>
<ID>6880</ID>
</Employee>
</documentroot>


Also to get your page to work in non-IE browsers, you should also code-in document.implementation.

Something to help you on your way:

<script type="text/javascript">
function XmlDOM()
{
this.onload=null;
this.document=null;
var t=this;
if (document.implementation && document.implementation.createDocument){
this.document = document.implementation.createDocument("", "", null);
this.document.onload = function(){if(t.onload)t.onload()};
}

else if (window.ActiveXObject){
this.document = new ActiveXObject("Microsoft.XMLDOM");
this.document.onreadystatechange = function () {if (t.document.readyState == 4)if(t.onload)t.onload();}
}
}
</script>
<script type="text/javascript">
var xmlpage=new XmlDOM();
xmlpage.onload=function(){
alert(xmlpage.document.documentElement.nodeName)
}
xmlpage.document.load("users.xml");
</script>

markem
05-15-2006, 09:45 AM
Thanks for the suggestions. I made the change to the XML file but I still can't seem to load/read the document. Any more ideas? My JS is somewhere between novice and intermediate, but the errors I get seem to indicate that the xmlDoc object is not defined in parseXML(). Would that be a reasonable assumption?

I appreciate the starter for making this browser independant. I'll work on that once I get the data to import.