Using Javascript to Create a XML Document from a HTML Form - javascript

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?

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.

Related

Getting text from a webpage using XMLHttpRequest - JavaScript

I'm trying to get text from a simple webpage using JavaScript and XMLHttpsRequest:
function test(){
const Http = new XMLHttpRequest();
const url='https://forecast.weather.gov/product.php?site=BOX&issuedby=BOX&product=AFD&format=CI&version=1&glossary=1&highlight=off';
Http.open("GET", url);
Http.send();
Http.onreadystatechange = (e) => {
console.log(Http.response)
}
let disc_text = document.getElementById("test_text")
var parser=new DOMParser();
var htmlDoc=parser.parseFromString(Http.response,"text/html");
var ps = htmlDoc.textContent;
disc_text.textContent = ps;
console.log(ps)
I'm expecting htmlDoc.textContent to return the text of the webpage, but instead it is null. I'm running the script locally via an HTML document. What's going wrong?

Check if 2 XML documents are identical with javascript

I have 2 xml documents stored which I get from an AJAX post request and I would like to check if they are the same. Obviously xml1 == xml2 is not working. Is there another way that I could make this work?
Try this. It parses the XML document using the method in this question and compares the two using isEqualNode.
function parseXMLString(xmlString) {
var xmlDoc;
if (window.DOMParser) {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlString, "text/xml");
} else // Internet Explorer
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xmlString);
}
return xmlDoc;
}
var xmlObj1 = parseXMLString('<hello>world</hello>');
var xmlObj2 = parseXMLString('<hello>world</hello>');
var xmlObj3 = parseXMLString('<hello>world2</hello>');
var xmlObj4 = parseXMLString('<hello2>world</hello2>');
console.log(xmlObj1.isEqualNode(xmlObj2));
console.log(xmlObj1.isEqualNode(xmlObj3));
console.log(xmlObj1.isEqualNode(xmlObj4));
If you're using jQuery, you can parse the XML document using parseXML().

Looking for a piece of code working in IE 10 and Firefox loading xml on local hard-drive

This is my working piece of code for IE<10, FF and Safari.
var xmlDoc;
function importXML(xmlfile)
{
var element;
var xmlloaded = false;
try
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", xmlfile, false);
}
catch (Exception)
{
var ie = (typeof window.ActiveXObject != 'undefined');
if (ie)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
while(xmlDoc.readyState != 4) {};
IE10 give me error in the following line of code: doesn't support load
xmlDoc.load(xmlfile);
element = xmlDoc.documentElement;
xmlloaded = true;
}
else
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = element = xmlDoc.documentElement;
xmlDoc.load(xmlfile);
xmlloaded = true;
}
}
if (!xmlloaded)
{
xmlhttp.setRequestHeader('Content-Type', 'text/xml')
xmlhttp.send("");
xmlDoc = xmlhttp.responseXML;
element = xmlDoc.documentElement;
xmlloaded = true;
}
}
I need this to work with IE 10 too.
I know the solution is here http://blogs.msdn.com/b/ie/archive/2012/07/19/xmlhttprequest-responsexml-in-ie10-release-preview.aspx because in another question on stackvoerflow a user said he found a solution there but he didn't wrote the mod.
I didn't write myself the above code and I'm a beginner so I don't know how to operate.
Javascript can't access the local file system for security reasons. If you have access to the required file and a server of some type, put the file on the server and access it from there using a URL.

load and parse xml from localstorage

I have the following, which loads XML from a web site and parses it:
function load() {
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = parse;
xhttp.open('GET', 'http://...XML.xml', false);
xhttp.send();
}
function parse() {
xmlDoc = xhttp.responseXML.documentElement.childNodes;
for (var i = 0; i < xmlDoc.length; i++) {
nodeName = xmlDoc[i].nodeName;
...
}
After I loading this, I store it in localStorage and I can retrieve it as a string. I need to be able to convert it back to a xml document just like:
xmlDoc = xhttp.responseXML.documentElement.childNodes;
does, so i can parse it. I have been looking for awhile now and can not figure it out.
Thanks in advance.
Based on the answer here XML parsing of a variable string in JavaScript Credit to #tim-down
You need to create an XML parser. Then pass the string into your parse instance. Then you should be able to query it as per before.
var parseXml;
if (typeof window.DOMParser != "undefined") {
parseXml = function(xmlStr) {
return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
};
} else if (typeof window.ActiveXObject != "undefined" &&
new window.ActiveXObject("Microsoft.XMLDOM")) {
parseXml = function(xmlStr) {
var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xmlStr);
return xmlDoc;
};
} else {
throw new Error("No XML parser found");
}
Example usage:
var xml = parseXml("[Your XML string here]");

Javascript permission denied error in IE , ActiveXObject is not defined in firefox while creating xml file

I want to create xml file by DOM, that is what i wrote.
I run it in IE and it brings out a Permission denied error, and ActiveXObject is not defined in firefox in that line:
fso = new ActiveXObject("Scripting.FileSystemObject");
How can I fix that??
var xhttp;
try {
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (f) {
xhttp = null;
}
}
if (!xhttp && typeof XMLHttpRequest != "undefined") {
xhttp = new XMLHttpRequest();
}
xhttp.open("GET", "nn.xml", false);
xhttp.send();
xmlDoc = xhttp.responseXML;
newel = xmlDoc.createElement("student");
newtext = xmlDoc.createElement("stName");
newel.appendChild(newtext);
newtext2 = xmlDoc.createElement("examName");
newel.appendChild(newtext2);
newtext3 = xmlDoc.createElement("grade");
newel.appendChild(newtext3);
x = xmlDoc.documentElement;
x.appendChild(newel);
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fso.OpenTextFile("D:\\test\\test\\nn.xml", 2, true, -1); //2=Open a file for writing.
ts.Write(xmlDoc.xml);
ts.Close();
ActiveX is for Microsoft browsers only, you should NOT be using that if you want cross browser compatibility.
As for writing to a file, this is not allowed for a good reason. Consider how many times your hard-drive would have been erased while surfing random internet sites, if this was allowed.

Categories

Resources