Tauntaunwampa
Tauntaunwampa

Reputation: 61

Using Javascript to Create a XML Document from a HTML Form

I'm having a lot of difficulty with this project. My aim is to write the results of a HTML form to an XML Document using Javascript.I have absolutely no idea how to do it.

Reason why I'm coming here is that I want to be sure that I'm on the right track. So far, I'm writing only one line "\n" just to test things out.

Here is my current JavaScript

var xhr = false;
if (window.XMLHttpRequest)
{
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}

function StoreRegXml()
{
xhr.open("GET", "php.php?" + Number(new Date), true);
xhr.onreadystatechange = getData;
xhr.send(null);
}

function getData()
{   
if ((xhr.readyState == 4) && (xhr.status == 200))
{
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var filename = "customer.xml";
    var file = fso.CreateTextFile(filename, true);
    file.WriteLine('<?xml version="1.0" encoding="utf-8"?>\n');
    file.Close();
}
}

Am I on the right track?

Edit: I'm adding alerts('test1'); to see where the code is going wrong and it stops at

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

Any ideas?

Upvotes: 0

Views: 6176

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167436

Inside the browser to create and populate an XML DOM document you can use the W3C DOM APIs with e.g.

var xmlDoc = document.implementation.createDocument(null, 'root', null);

var foo = xmlDoc.createElement('foo');
foo.textContent = 'bar';

xmlDoc.documentElement.appendChild(foo);

console.log(xmlDoc);

This creates an in memory XML DOM document, not an XML file. You can then for instance send the xmlDoc with XMLHttpRequest to the server.

Upvotes: 1

Related Questions