Reading data from user-submitted XML file.

L

L. Ximenes

Hello everyone,

I know how to parse data from an XML file with Javascript; but what if
I would like to parse data from a user-submitted XML file via the "
<input type='file'> "? Would it be possible to read directly from that
file, or would I need the user to upload the file and subsequently
read the data from it? In any case, what would be the best and cleaner
way to do it?
I thank you in advance for any insight on this,

Sincerely,

L.
 
E

Evertjan.

L. Ximenes wrote on 28 okt 2008 in comp.lang.javascript:
I know how to parse data from an XML file with Javascript; but what if
I would like to parse data from a user-submitted XML file via the "
<input type='file'> "? Would it be possible to read directly from that
file, or would I need the user to upload the file and subsequently
read the data from it? In any case, what would be the best and cleaner
way to do it?

<input type='file'> is an uploading process.

Under normal security settings a browser cannot and should not read files
from the own client computer.

Your quest can greatly benifit from a serverside solution, as I point out
in my response to your other posting.

In IE, HTA instead of HTML could do the job too, I suppose,
but only on your own machine, not via the web.
 
E

Erwin Moller

L. Ximenes schreef:
Hello everyone,

I know how to parse data from an XML file with Javascript; but what if
I would like to parse data from a user-submitted XML file via the "
<input type='file'> "? Would it be possible to read directly from that
file, or would I need the user to upload the file and subsequently
read the data from it? In any case, what would be the best and cleaner
way to do it?
I thank you in advance for any insight on this,

Hi,

You'll have to do this serverside.
If you want to read a file on the clients machine, JavaScript cannot
access it under most circumstances.
Maybe it is possible to lower securitysettings in IE to such a low level
it is allowed, I am not sure if that is possible.
(Just imagine how safe your computer would be if any webpage could
access files on your computer via JavaScript.)

If you REALLY want to do the parsing clientside with JavaScript, you
could use the following round-trip:
1) Upload the file to the server.
2) Server places the file in a place where it is accessible via http.
3) Let JavaScript get the file in (using AJAX eg) from that location.
4) Do the parsing in JavaScript.

But I would always do this serverside. Powerful languages exist that can
make this easy for you (eg PHP, Perl).

Regards,
Erwin Moller

Sincerely,

L.


--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
E

Evertjan.

Erwin Moller wrote on 28 okt 2008 in comp.lang.javascript:
But I would always do this serverside. Powerful languages exist that can
make this easy for you (eg PHP, Perl).

Serverside Javascript being such powerful language, toch, Erwin?

The OP just needs to port his clientside solution to the server, with minor
adjustments.

And he stays being on topic in this NG as an additional bonus. ;-)
 
E

Erwin Moller

Evertjan. schreef:
Erwin Moller wrote on 28 okt 2008 in comp.lang.javascript:


Serverside Javascript being such powerful language, toch, Erwin?

Absolutely Evertjan,

At least, that is what I heard (from you amongst others).
Personally, I never wrote 1 line of serverside JavaScript. I prefer PHP
or Java (and hate Perl).
The OP just needs to port his clientside solution to the server, with minor
adjustments.

And he stays being on topic in this NG as an additional bonus. ;-)

OK, but didn't you like my absurd suggestion: roundtrip to the server,
and then import it via XHR back to the JavaScript on the client? ;-)

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
E

Evertjan.

Erwin Moller wrote on 28 okt 2008 in comp.lang.javascript:
Evertjan. schreef:

Absolutely Evertjan,

At least, that is what I heard (from you amongst others).
Personally, I never wrote 1 line of serverside JavaScript. I prefer
PHP or Java (and hate Perl).

It is very easy, if you are used to ASP and javascript.

Even the porting and repeating of functions that do not need special
browser [DOM, etc] or server support is nice.

checking for an acceptable form input can be repeated client and
serverside by just using exactly the same function on both sides.
OK, but didn't you like my absurd suggestion: roundtrip to the server,
and then import it via XHR back to the JavaScript on the client? ;-)

Certainly in general.

But why not process these data serverside,
forgoing the OP's stated cross-browser problem?

Especially if the javascript code is already available to the OP.
 
T

Thomas 'PointedEars' Lahn

L. Ximenes said:
I know how to parse data from an XML file with Javascript; but what if I
would like to parse data from a user-submitted XML file via the " <input
type='file'> "? Would it be possible to read directly from that file,

Security restrictions usually prevent you from accessing local files
(file://) from remote resources (http(s):// or another non-file URI).
There are ways to work around that, but those are not interoperable.
They tend to be considered security bugs by vendors, so they have been "fixed".

However, if your application is running on the local filesystem, it may be
possible to make an emulated HTTP request using the file's URI to get its
file's content. In MSHTML, you need to use the
ActiveXObject("Microsoft.XMLHTTP") constructor call instead of the
XMLHttpRequest() constructor call for this to work.
or would I need the user to upload the file and subsequently read the
data from it?

AFAIK, that is the interoperable approach that always works. It should be
possible to use that as a fallback mechanism for the XHR approach.


PointedEars
 
J

Jorge

Hello everyone,

I know how to parse data from an XML file with Javascript; but what if
I would like to parse data from a user-submitted XML file via the "
<input type='file'> "? Would it be possible to read directly from that
file, or would I need the user to upload the file and subsequently
read the data from it? In any case, what would be the best and cleaner
way to do it?
I thank you in advance for any insight on this,

Sincerely,

You could put a textarea and ask the user to copy-paste the xml's text
into it ?
 

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

No members online now.

Forum statistics

Threads
474,137
Messages
2,570,802
Members
47,349
Latest member
eixataze

Latest Threads

Top