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

Writing an XML document via Javascript.

P: n/a
Greetings,

I've recently been struggling with the idea of writing an XML document
based on user-submitted content using javascript.
Using various instances of document.write on a newly opened window I
succeeded in the task of outputting a perfectly valid XML document,
although I have been experiencing a few problems.
If I look at the source code of the new window, what I have is a
perfect XML document, such that if I copy the source code to a text
file and name it with the extension .xml, everything goes smoothly. On
the othere hand, trying to save it directly from my web browser still
gives an HTML file, with the needed missing tags (such as HTML, HEAD,
BODY) being added automatically. Is there any way to prevent this and
save only the code as it is?
On a side note, Safari apparently doesn't see any source code in
Javascript generated windows... is it just me?

I kindly thank you in advance,

L.
Oct 28 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
L. Ximenes wrote on 28 okt 2008 in comp.lang.javascript:
I've recently been struggling with the idea of writing an XML document
based on user-submitted content using javascript.
Using various instances of document.write on a newly opened window I
succeeded in the task of outputting a perfectly valid XML document,
although I have been experiencing a few problems.
If I look at the source code of the new window, what I have is a
perfect XML document, such that if I copy the source code to a text
file and name it with the extension .xml, everything goes smoothly. On
the othere hand, trying to save it directly from my web browser still
gives an HTML file, with the needed missing tags (such as HTML, HEAD,
BODY) being added automatically. Is there any way to prevent this and
save only the code as it is?
On a side note, Safari apparently doesn't see any source code in
Javascript generated windows... is it just me?
It seems you want to use viewsource to copy the xml?

Why not use serverside javascript,
bypassing your cross-browser problem of doing this,
so just returning a complete xml document to the client?

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

P: n/a
L. Ximenes wrote:
Using various instances of document.write
Don't. One document.write() call is enough, more efficient, and less
error-prone.
on a newly opened window I succeeded in the task of outputting a
perfectly valid XML document, although I have been experiencing a few
problems. If I look at the source code of the new window, what I have is
a perfect XML document, [...] trying to save it directly from my web
browser still gives an HTML file, with the needed missing tags (such as
HTML, HEAD, BODY) being added automatically. Is there any way to prevent
this and save only the code as it is?
document.open("text/xml");

might help. Saving it as .xml instead of .html might help, too.
On a side note, Safari apparently doesn't see any source code in
Javascript generated windows... is it just me?
You have not provided enough information for an educated guess.

The FAQ is currently broken, but it stands to reason that you should at
least post the relevant lines of your source code.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Oct 29 '08 #3

P: n/a
First of all, thanks to both of you for the helpful and quick replies.

Evertjan wrote:
>It seems you want to use viewsource to copy the xml?
>Why not use serverside javascript,
bypassing your cross-browser problem of doing this,
so just returning a complete xml document to the client?
This would be exactly what I first tried to do. Alas, as you have
probably guessed, my skills are at a beginner level and all I did
manage to do was merely a work-around to my initial intention.
How would I do this, if I may ask??

Thomas 'PointedEars' Lahn wrote:
Don't. *One document.write() call is enough, more efficient, and less
error-prone.
Thank you for the suggestion why is that so? Anyhow, I used several
of them merely because some of them are just conditional. Maybe I
should create a variable in which to store all the text to write and
join all the bunches I have there. Then use a document.write()
instance to write the joined variable. Would that be cleaner?
* document.open("text/xml");

might help. *Saving it as .xml instead of .html might help, too.
Hmm... alas it didn't help, by trying it I effectively get an unparsed
document (while, not specifying it, the xml opened was still rendered
as HTML by the browser, even though it was not showing it in the
source code until download attempt). However, trying to save it, gives
me an html document formed as such:

<html><body><h2>Filename missing</h2></body></html>

Merely saving it as a .xml (as I hoped would work as part of the above-
mentioned work-around I not so wittingly devised) doesn't work either.
On a side note, Safari apparently doesn't see any source code in
Javascript generated windows... is it just me?

You have not provided enough information for an educated guess.

The FAQ is currently broken, but it stands to reason that you should at
least post the relevant lines of your source code.
Sorry if I didn't give any deeper information; posting specifically
the source code I am working on would be impossible, as it works in
tandem with a whole other code.
Anyhow, reproducing this bug is for me very simple; try it out with
this example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function test() {
new_window=window.open();
newdocument=new_window.document;
newdocument.write("Where is the source for this text,
Safari?");
newdocument.close();
}
</script>
</head>
<body>
<input type="button" value="Open" onclick="test()" />
</body>
</html>

Thank you very much again, I highly appreciate your support.

Regards,

L.
Oct 29 '08 #4

P: n/a
Greetings,

On 29 Ott, 12:48, Richard Cornford <Richard.Cornf...@googlemail.com>
wrote:
But then the problem of recovering the original XML source form the
document displaying it becomes moot as you can still get that original
source from - xmlArray.join('') -.
Thank you Richard for your in-depth informations. I may be slow, but I
still don't get how can I retrieve the xml source from that array and
display the new window as an xml file, as opposed to having the
default HTML opening mark-up which seems to always appear on a
javascript generated window.
Also, due to my lack of proper English skills (other than
Javascript ;)), I am not sure about what do you mean by "the problem
[...] becomes moot" (looking up dictionaries didn't help, that's why I
ask).
>
<snip>* newdocument.write("Where is the source for this text, Safari?");

<snip* * * * * * * * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The source is right there. You had it when you wrote it, so if you
don't have it later that is because _you_ threw it away.
This is again probably due to the above mentioned lack, but I also
don't get what do you mean by "I threw it away".
The problem I was addressing there is specifically that, if I look up
for the source of the newly opened window in Safari I get a completely
blank page, no trace whatsoever of the text written, nor of the
default HTML markup I mentioned above. As a matter of facts, trying to
save the new page in Safari gives an empty document if saved as a
WebArchive, and an error if saved as a source.
Whereas, in Firefox I can see the source as it is exactly imputed by
the document.write() function; on the other hand, downloading it, adds
the HTML markup to enclose that source.
I hope I made my problem clearer.
Thank you very much for the courtesy and attention.

Regards,

L.
Oct 29 '08 #5

P: n/a
Richard Cornford wrote:
On Oct 29, 8:58 am, L. Ximenes wrote:
<snip>
>Thomas 'PointedEars' Lahn wrote:
>>Don't. One document.write() call is enough, more efficient, and less
error-prone.
Thank you for the suggestion - why is that so?

More efficient is just the way it is (there are overheads in -
document.wirte - and repeating them is not efficient). As to "error-
prone", that is questionable.
Rendering and script runtime errors when writing incomplete elements have
been reported before, particularly in MSHTML.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Oct 31 '08 #6

P: n/a
L. Ximenes wrote:
I've recently been struggling with the idea of writing an XML document
based on user-submitted content using javascript.
Using various instances of document.write on a newly opened window I
succeeded in the task of outputting a perfectly valid XML document,
although I have been experiencing a few problems.
If I look at the source code of the new window, what I have is a
perfect XML document, such that if I copy the source code to a text
file and name it with the extension .xml, everything goes smoothly. On
the othere hand, trying to save it directly from my web browser still
gives an HTML file, with the needed missing tags (such as HTML, HEAD,
BODY) being added automatically. Is there any way to prevent this and
save only the code as it is?
Maybe the following could help:
http://groups.google.com/group/comp....bb901388011966

(document.execCommand is IE-only)

--
Bart
Oct 31 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.