473,385 Members | 1,587 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,385 software developers and data experts.

Writing an XML document via Javascript.

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
6 11704
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Christoph Bergmann | last post by:
Hi... We want to write an open source web based TEXT editor and would be happy about any help ;-) Please notice: We do NOT want to write a web based WEB editor, where you can edit a web...
6
by: sentinel | last post by:
Hi, I’m trying to modify a DHTML editor that parses a style-sheet via PHP and instead of modifying the tags via execCommand(), find a way of writing inline styles by way of adding <span style=>...
12
by: mammothman42 | last post by:
hi ppl i've made a javascript component in a html file, and i want to be able to use this on other pages, so i'm going to put it into a js file. thing is, the component in 90% html and 10% java....
1
by: Michel | last post by:
I'm looking for a javascript writing tool that helps you with displaying all the different things that could follow after the dot. Like if you would type "document." then there would popup a list...
9
by: Robby Bankston | last post by:
I'm working on some code and am running into brick walls. I'm trying to write out Javascript with Javascript and I've read the clj Meta FAQ and didn't see the answer, read many similar posts (with...
2
by: johkar | last post by:
I am getting an Access denied error when I write to a new window. The situation and code are outlined below. I am setting the domain in the main window. The problem is that the window I am...
15
by: Cerebral Believer | last post by:
Hi all, I am a newbie to JavaScript and am just trying to get a script going that will write the ful date and time to each webpage as it is viewed. Can anyone point out what mistakes there are...
12
by: LayneMitch via WebmasterKB.com | last post by:
Hello everyone. I'm currently learning Javascript and doing a few exercises. One problem I'm working on takes an array of names from an xml file using Ajax and writes it to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.