Writing an XML document via Javascript.

L

L. Ximenes

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

Evertjan.

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?
 
T

Thomas 'PointedEars' Lahn

L. Ximenes said:
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
 
L

L. Ximenes

First of all, thanks to both of you for the helpful and quick replies.
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??
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.
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.
 
R

Richard Cornford

Don't. One document.write() call is enough, more efficient, and less
error-prone.

Thank you for the suggestion - why is that so?[/QUOTE]

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

Almost certainly. The usual buffer for that type of operation would be
an array, so:-

var xmlArray = [];

- and then instead of the individual - document.write - calls
something like:-

xmlArray[xmlArray.length] = 'whatever was passed to document.write';

- or:-

xmlArray.push('whatever was passed to document.write');

- and then at the end using:-

document.write(xmlArray.join(''));

- where - xmlArray.join('') - returns a string that each string value
stored in the array, in order, joined together using the empty string
(so effectively just joined together). This concatenation of all of
the contents of the array then becomes your XML source.

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('') -.

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.

Richard.
 
L

L. Ximenes

Greetings,

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

Thomas 'PointedEars' Lahn

Richard said:
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.[/QUOTE]

Rendering and script runtime errors when writing incomplete elements have
been reported before, particularly in MSHTML.


PointedEars
 
B

Bart Van der Donck

L. Ximenes said:
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.lang.javascript/msg/7fbb901388011966

(document.execCommand is IE-only)
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top