Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libraries to write xml with JavaScript

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!?

  • http://goessner.net/download/prj/jsonxml/ (LGPL)
  • not yet released: https://sourceforge.net/projects/jsonix (LGPL)

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');
like image 385
Karussell Avatar asked Oct 31 '10 22:10

Karussell


People also ask

Is there a standard library for JavaScript?

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.

How can we add a library in JavaScript?

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.

How does node js work with XML?

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.

What are library files in JavaScript?

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.


1 Answers

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.

like image 146
Wahid Kadwaikar Avatar answered Oct 06 '22 04:10

Wahid Kadwaikar