473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help: reading an XML file

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')">

May 4 '06 #1
7 4756


me******@westernsurety.com wrote:

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>


--

Martin Honnen
http://JavaScript.FAQTs.com/
May 5 '06 #2
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)

May 5 '06 #3


me******@westernsurety.com wrote:

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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
May 6 '06 #4
>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);
}

May 8 '06 #5
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>

May 10 '06 #6
me******@westernsurety.com wrote:
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.

--
Dag.

May 10 '06 #7
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);
}

May 10 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: nephish | last post by:
Hey there, i am trying to write an online application using the cgi module. what i want to do is have an html form display a drop-down list and have the values of that list be the lines of text...
1
by: Hafeez | last post by:
I am having real trouble compiling this code http://www.cs.wisc.edu/~vganti/birchcode/codeHier/AttrProj.tgz The attachment shows errors when compiled using the current version of g++ in a...
2
by: Parker.Jim | last post by:
I need to write a program which performs word subsitutions on a text file. The program should input the names of three text files: the source file that will be "edited", a text file that contains...
3
by: Chi | last post by:
what is the "unable to write data to the transport connection" I use the oreilly , programming c# using System; using System.Net.Sockets; using System.Text; using System.IO; // get a file...
36
by: felixnielsen | last post by:
What i really wanna do, is defining my own types, it doesnt really matter why. Anyway, i have run into some problems 1) typedef unsigned short U16; U16 test = 0xffffffff; // There should be a...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
2
by: -D- | last post by:
I'm taking my first stab at using xml, so please bear with my novice questions and understanding of xml. I'm trying to create an xml file that holds all my website navigation. If I understand...
1
by: syhzaidi | last post by:
How can we do Parsing of Hexdecimel in C# reading string from stream file for eg.. i have a file like.......... 0f 2f 12 2d 3a.......in hexa decimal save in a file.txt and i m reading it from...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.