Use Javascript to assemble a XML file?

T

TPK

Is there any way javascript can be used to assemble a XML file?

Any resources out there that may explain how to do this (if possible)?

TPK
 
J

Julian Turner

TPK said:
Is there any way javascript can be used to assemble a XML file?

Any resources out there that may explain how to do this (if possible)?

TPK

Hi

The answer is yes.

JavaScript generally has no native XML handling (although there is an
extension to Firefox which implements E4X I think :
<URL:http://www.ecma-international.org/publications/standards/Ecma-357.htm>)

However, the latest web browsers all provide a host objects/ActiveX
objects which Javascript can access to create XML documents.

If I type in "Javascript and XML" in google, I get approximately 34
million responses, and there are probably just as many posts on the
subject if you search this newsgroup. I suggest you start by doing
some searching and following up a few of those yourself.

The cross-browser Sarissa Javascript library is also useful to look at,
or the zXML library provided by <URL:http://www.nczonline.net>.

Regards

Julian Turner
 
M

Martin Honnen

TPK said:
Is there any way javascript can be used to assemble a XML file?

Any resources out there that may explain how to do this (if possible)?

You can use the DOM to build an in memory XML DOM document. Saving to a
local file is usually (meaning in the browser sandbox) not possible but
you can use XMLHttpRequest so send such a DOM document to your server.

var xmlDocument = null;

if (document.implementation && typeof
document.implementation.createDocument != 'undefined') {
xmlDocument = document.implementation.createDocument('', 'dummy', null);
}
else if (typeof ActiveXObject != 'undefined') {
try {
xmlDocument = new ActiveXObject('Msxml2.DOMDocument');
}
catch (e) {
try {
xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
}
catch (e) {}
}
}

if (xmlDocument != null) {
var root = xmlDocument.createElement('gods');
if (xmlDocument.documentElement) {
xmlDocument.replaceChild(root, xmlDocument.documentElement);
}
else {
xmlDocument.appendChild(root);
}
var god = xmlDocument.createElement('god');
god.appendChild(xmlDocument.createTextNode('Kibo'));
root.appendChild(god);
if (typeof XMLSerializer != 'undefined') {
alert(new XMLSerializer().serializeToString(xmlDocument));
}
else {
alert(xmlDocument.xml);
}
}

createDocument is W3C DOM Level 2 Core to create an XML DOM document.
That method wants the tag name (and namespace) of the root element so
the newly created document has already a root element. The method is
supported by browsers like Mozilla/Firefox, Opera 8/9.
new ActiveXObject('Msxml2.DOMDocument') (respectively new
ActiveXObject('Microsoft.XMLDOM')) is for IE/Win using MSXML to create
an XML DOM document which is empty.
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top