XMLHttpRequest for *creating* XML documents?

W

William

Heya,

I have been looking all over Google for ways to *create* an XML document
using any Javascript object, and it seems that no one has ever tried or
ever needed this.

I want to send the server some info dynamically created on the page, and
instead of sending a complicated GET request with the info, or creating
an XML using my own set of functions, is there any object that allows
you to use the Document Object Model, like adding nodes, etc, and then
output the XML text at the end you could then send to the server? It
seems XMLHttpRequest is only for requests, and I don't want to use
Microsoft-only ActiveX XML objects.

Any idea?

Thanks,
 
N

NetOne

XMLHttpRequest is supported on various browsers nowadays, although in
different ways.
 
R

RobG

William said:
Heya,

I have been looking all over Google for ways to *create* an XML document
using any Javascript object, and it seems that no one has ever tried or
ever needed this.

Look a little harder ;-) try the W3 Load and Save spec:

"a platform- and language-neutral interface that allows programs
and scripts to dynamically load the content of an XML document
into a DOM document and serialize a DOM document into an
XML document"

<URL:http://www.w3.org/TR/DOM-Level-3-LS/>

However it's not well supported by browsers yet - at least it gives you
a starting point. There's some info here:

<URL:http://developer.mozilla.org/en/docs/XML_in_Mozilla#DOM_Load_and_Save_Methods>
 
L

Laurent Bugnion

Hi,
Heya,

I have been looking all over Google for ways to *create* an XML document
using any Javascript object, and it seems that no one has ever tried or
ever needed this.

I want to send the server some info dynamically created on the page, and
instead of sending a complicated GET request with the info, or creating
an XML using my own set of functions, is there any object that allows
you to use the Document Object Model, like adding nodes, etc, and then
output the XML text at the end you could then send to the server? It
seems XMLHttpRequest is only for requests, and I don't want to use
Microsoft-only ActiveX XML objects.

Any idea?

Thanks,

The XML objects are not IE only, see
http://www.quirksmode.org/dom/importxml.html
for a cross-browser example. I implemented the example and tested in
Firefox, it works. Is that what you're looking for?

Greetings,
Laurent
 
M

Martin Honnen

William wrote:

I have been looking all over Google for ways to *create* an XML document
using any Javascript object, and it seems that no one has ever tried or
ever needed this.


Mozilla and Opera support the W3C DOM Level 2
var xmlDocument = document.implementation.createDocument('',
'root-element', null)
to start from scratch.

Then they both support DOMParser to start with parsing a string e.g.
var xmlDocument = new DOMParser().parseFromString(
'<gods><god>Kibo</god></gods>',
'application/xml'
);
 
W

William

Thanks, but if you read my message, I was saying that it doesnt support
creating XML documents, which is really want I want to do....
 
W

William

Thanks for the answer, but I've seen this before, and again, I want to
*create* and *save* a document in a browser in Javascript, not load one,
and this example doesnt explain creating and saving an XML, just loading
one. I need something like:

var obj = new XMLDoc();
var item = obj.AddNode(a);
var item2 = obj.AddNode(b);
var xml = obj.contentXml;

and then be able to send this to a PHP page through XMLHttpRequest or
whatever.

So please if someone ever created and saved in a variable some XML text
created using DOM on a browser, let me know. I'm not talking about
making my own function to create the XML, but this is what I might end
up doing. I just thought it would be cleaner to use a DOM object, which
is more consistent with other widely used techniques.
 
M

Martin Honnen

William wrote:

I need something like:

var obj = new XMLDoc();

var xmlDoc = document.implementation.createDocument('',
'root-element', null);
var item = obj.AddNode(a);

var element = xmlDoc.createElement('child');
element.appendChild(xmlDoc.createTextNode('Kibology for all.'));
xmlDoc.documentElement.appendChild(element);
and then be able to send this to a PHP page through XMLHttpRequest

The send method takes a DOM node like a DOM XML document as the argument
so you can POST the xmlDoc created above to the server.

Code works with Mozilla and Opera at least, not sure about Safari,Konqueror.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top