473,750 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11728
L. Ximenes wrote on 28 okt 2008 in comp.lang.javas cript:
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.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function test() {
new_window=wind ow.open();
newdocument=new _window.documen t;
newdocument.wri te("Where is the source for this text,
Safari?");
newdocument.clo se();
}
</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.c om>
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.wri te("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.execC ommand 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
2151
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 page without knowing HTML - we want to write a real programmer's editor located on a web page in your browser with features like...
6
10843
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=> tags around the selected text. Easier said than done. My first though was to do a search and replace on the innerHTML, but falls down if there is more than one match (it will only modify the first occurrence). I suspect that even if I could
12
1570
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. so how do i print out a whole slab of html without having to do a hideous batch of document.write? couldn't figure out how other components did it. cheers dave
1
1511
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 with "all, getElementById, open, write, ...." to choose from. It would even be better if it also has a line/line debugger which also adds even more present objects to choose from. This would really help me writing some javascript. Thanks!
9
2896
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 no luck though), and searched through the IRT.ORG Faqs (www.irt.org/script/script.htm). The Javascript is designed to open an popup window and then inside that window call another script which will resize that window. There may be another...
2
22649
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 popping up never gets it domain set, even though I have coded it to be the same as the main window. I get an Access Denied errror. Four different domains: *localhost.mycompany.com *test.mycompany.com *pilot.mycompany.com
15
2337
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 in my code, as it does not seem to work? I am actually trying to learn what parts of the code do what and why, so if anyone can take the time to explain it would be much appreciated. Here is my script: <script language="javascript">
12
2332
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 <select<optionstags. This is the code they use:
0
8999
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.