I am doing some server side coding with JavaScript (node.js) and I would like to write valid xml.
I found two libs, but I am sure there are more/better!?
Requirements: open source (for commercial usage)
Would be cool if the project is fast, small and simple to use (in that order). And I would like to have a bit lower level access ala
doc.addElement('xy').addAttr('name', 'bob');
The JavaScript language does not have a standard library. As a result new functionality is added to the global object, or developers find and adopt libraries that they bundle with the rest of their application.
Use the Add . js file to browse and select a . js file to add to your JavaScript user library. Use the Add folder button to browse and select a folder to add to your JavaScript user library.
The installed module exposes the Parser object, which is then used to read XML. const xml2js = require('xml2js'); const fs = require('fs'); const parser = new xml2js. Parser({ attrkey: "ATTR" }); // this example reads the file synchronously // you can read it asynchronously also let xml_string = fs. readFileSync("data.
In JavaScript, the way we do that is by using a library. A library is a JavaScript file that contains a bunch of functions, and those functions accomplish some useful task for your webpage.
I've created two functions as follows:
function loadXMLDoc(filename){
if (window.XMLHttpRequest){
xhttp=new XMLHttpRequest();
}
else {
xhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE 5-6
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
And, to write the XML into a local file call the following function.
function writeXML()
{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FILENAME="D:/YourXMLName/xml";
var file = fso.CreateTextFile(FILENAME, true);
file.WriteLine('<?xml version="1.0" encoding="utf-8"?>\n');
file.WriteLine('<PersonInfo>\n');
file.WriteLine('></Person>\n');
}
file.WriteLine('</PersonInfo>\n');
file.Close();
}
I hope this helps, or else you can try Ariel Flesler's XMLWriter for creating XML in memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With