Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / October 2008



Tip: Looking for answers? Try searching our database.

Writing an XML document via Javascript.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
L. Ximenes - 28 Oct 2008 08:14 GMT
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.
Evertjan. - 28 Oct 2008 09:44 GMT
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.
[quoted text clipped - 10 lines]
> 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?

Signature

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Thomas 'PointedEars' Lahn - 29 Oct 2008 01:52 GMT
> 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
[quoted text clipped - 3 lines]
> 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
Signature

   realism:    HTML 4.01 Strict
   evangelism: XHTML 1.0 Strict
   madness:    XHTML 1.1 as application/xhtml+xml
                                                   -- Bjoern Hoehrmann

L. Ximenes - 29 Oct 2008 09:58 GMT
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??

> 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?
[quoted text clipped - 3 lines]
> 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.
Richard Cornford - 29 Oct 2008 12:48 GMT
<snip>
>> 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.

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

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

Richard.
L. Ximenes - 29 Oct 2008 18:47 GMT
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.
Thomas 'PointedEars' Lahn - 31 Oct 2008 02:52 GMT
> <snip>
>>> Don't.  One document.write() call is enough, more efficient, and less
[quoted text clipped - 4 lines]
> 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
Signature

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

Bart Van der Donck - 31 Oct 2008 11:27 GMT
> I've recently been struggling with the idea of writing an XML document
> based on user-submitted content using javascript.
[quoted text clipped - 8 lines]
> 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)

--
Bart
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.