Libraries to write xml with JavaScript

19,593

Solution 1

There are a number of XML libraries for node.js listed at http://github.com/ry/node/wiki/modules#parsers-xml

If memory serves, the one that has the most traction is http://github.com/polotek/libxmljs, which appears to be MIT licensed.

Solution 2

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.

Solution 3

I recently released node-genx, a wrapper around a small C-library called Genx that provides fast and valid xml generation in node.js.

Installation is simply:

npm install genx

I have posted some examples of using it to generate an Atom feed and a Sphinx xmlpipe2 stream on my blog.

Solution 4

I've found Ariel Flesler's XMLWriter constructor function to be a good start for creating XML from scratch (in memory), take a look at this

http://flesler.blogspot.com/2008/03/xmlwriter-for-javascript.html

Example

function test(){    
   // XMLWriter will use DOMParser or Microsoft.XMLDOM
   var v = new  XMLWriter();
   v.writeStartDocument(true);
   v.writeElementString('test','Hello World');
   v.writeAttributeString('foo','bar');
   v.writeEndDocument();
   console.log( v.flush() );
}

Result

<?xml version="1.0" encoding="ISO-8859-1" standalone="true" ?>
<test foo="bar">Hello World</test>

A couple of caveats, it doesn't escape strings and the syntax can get coyote++ ugly. You can download it from the author's site or from https://github.com/alexandern/XMLWriter (includes escaping and bug fix for the standalone attribute)

Share:
19,593
Karussell
Author by

Karussell

Co-Founder of GraphHopper. Try the route planner demo based on the GraphHopper Directions API.

Updated on July 20, 2022

Comments

  • Karussell
    Karussell almost 2 years

    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');