By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 1,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

Reading data from user-submitted XML file.

P: n/a
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.
Oct 28 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 28 '08 #2

P: n/a
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
Oct 28 '08 #3

P: n/a
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. ;-)

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 28 '08 #4

P: n/a
Evertjan. schreef:
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?
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
Oct 28 '08 #5

P: n/a
Erwin Moller wrote on 28 okt 2008 in comp.lang.javascript:
Evertjan. schreef:
>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?

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.
>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? ;-)
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.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 28 '08 #6

P: n/a
L. Ximenes wrote:
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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Oct 28 '08 #7

P: n/a
On Oct 28, 3:05*am, "L. Ximenes" <theosko...@gmail.comwrote:
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 ?

--
Jorge.
Oct 28 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.