Help: reading an XML file

M

memiller

This is my first experience working with XML. I'm trying to write code
that will read an XML file. I've found several examples but I can't
get them to work. Am I missing a DLL file? The errors I commonly get
are "object required" (as with the code below) or "permission denied".
Any insight would be helpful and appreciated. Here is what I'm trying:

<script language="JavaScript">
function importXML(file)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) createTable()
};
xmlDoc.loadXML(file);
}

function createTable()
{
var doc=xmlDoc.documentElement;
var x = xmlDoc.getElementsByTagName("Employee");
for (i=0;i<x[0].childNodes.length;i++)
{
alert(i); //enter code to process stuff here
}
}
</script>

<INPUT TYPE=button VALUE="import XML"
onClick="importXML('C:\tmpzip\test1.xml')">
 
M

Martin Honnen

xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) createTable()
};
xmlDoc.loadXML(file);
onClick="importXML('C:\tmpzip\test1.xml')">

If you want (with IE and MSXML) to parse a string with XML markup then
you need to loadXML method. However your code indicates you want to load
from a file with markup, then you need to load method e.g.
xmlDoc.load(file)
and
importXML('C:\\dir\\file.xml')
which however is only allowed if you script runs locally.
If your HTML document with the script has been loaded from a HTTP server
(e.g. http://example.com/) then you can load XML documents from that
server e.g.
importXML('file.xml')
or
importXML('http://example.com/dir/subdir/file.xml')

Generally on the web if you want to load XML cross browser then the best
way to do that is in my view XMLHttpRequest as that way once the object
has been created you have the same API in IE/Win, Mozilla, Opera, Safari:
<http://www.faqts.com/knowledge_base/view.phtml/aid/6826/fid/616>
 
M

memiller

Martin,

Thanks for the info. Can you clarify your statement about the script
running locally? My scenario is this: From the corporate intranet, I
need provide the ability for a user to upload an XML file from which
data will be processed and saved into the database. The web page is
ASP, but not .NET. If I provide a button on the web page to activate
the JavaScript which imports the file, would that be the same as
running locally? This project won't fly if the user has to save the
XML file to the server. (And that leads me to the next hurdle -
providing a dialog to select the file to upload)
 
M

Martin Honnen

My scenario is this: From the corporate intranet, I
need provide the ability for a user to upload an XML file from which
data will be processed and saved into the database.


Use the file input control e.g.
<form method="POST"
enctype="multipart/form-data"
action="saveFile.asp">
<input type="file" name="fileName">
<input type="submit">
</form>
if you want the user to select and upload a file from the local file system.
 
M

memiller

Use the file input control e.g.

Martin,

Thanks for the snippet. Gets me that much closer, but I'm still
struggling with parsing the XML document. Is there an online command
reference somewhere that lists what methods are available and thier
description? For example: .documentElement - I can't seem to get a
value to return when I use this in the example below, likewise with
..getElementsByTagName, but the .readyState does work.

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

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

function createTable()
{
alert("readyState=" + xmlDoc.readyState);
var doc=xmlDoc.documentElement;
alert(xmlDoc.documentElement);
var x = xmlDoc.getElementsByTagName("Employee");
alert("ElementsByTagName=" + x);
}
 
M

memiller

The following code gives me the errors:

alert("Error=" + xmlDoc.parseError.reason + "Line=" +
xmlDoc.parseError.line + "txt=" + xmlDoc.parseError.scrText);

"Invalid at the top level of the document."
"Line=1"
"Object undefined."

Here is my data:

<?xml version="1.0"?>
<Employee>
<ID>6880</ID>
<CostCenter>Properties</CostCenter>
<Name>VP, Ferlin</Name>
<JobTitle>Facilities Technician</JobTitle>
<JobGrade>120</JobGrade>
</Employee>
 
D

Dag Sunde

The following code gives me the errors:

alert("Error=" + xmlDoc.parseError.reason + "Line=" +
xmlDoc.parseError.line + "txt=" + xmlDoc.parseError.scrText);

"Invalid at the top level of the document."
"Line=1"
"Object undefined."

Here is my data:

<?xml version="1.0"?>
<Employee>
<ID>6880</ID>
<CostCenter>Properties</CostCenter>
<Name>VP, Ferlin</Name>
<JobTitle>Facilities Technician</JobTitle>
<JobGrade>120</JobGrade>
</Employee>

Probably got nothing to do with your xml...

xmlDoc is never instantiated, and results in a "Object undefined."
error.
 
M

memiller

Thanks for the response, Dag.

I see your reasoning, but then I'm confused why the following code
fails to return a value:

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

xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false"
xmlDoc.loadXML(fileName);
alert(xmlDoc.documentElement);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top