Libraries to write xml with JavaScript
Asked Answered
F

5

11

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');
Fuzzy answered 31/10, 2010 at 22:2 Comment(7)
Option 1: document.write('<xy name="' + html('bob') + '">') Option 2: write a library yourself, you wouldn't believe how easy it is. All you'd need would be a function that properly escapes HTML entities (angle brackets etc). Just don't use namespaces, they make both the library and the whole thing a lot more complicated. Good thing about this would be (1) that you'd know exactly what the library is doing and (2) you will have the simplest possible library that does the job specifically for you.Demurral
But I cannot imagine that there isn't a single lib which already does this ... exactly that is the problem: "angle brackets etc" :-)Fuzzy
@Karussell: (please do reply with @Demurral in the beginning, looks like that's when SO sends me a notification). The problem with almost all these libraries is that they do a lot more than you usually need. As for HTML escape, here is how to do it: function html(s) { return s.split("&").join("&amp;").split( "<").join("&lt;").split(">").join("&gt;") } (sorry, formatting is not available here)Demurral
Actually my html() is incomplete without escaping the double quotes too. In other words you only need to escape chars that affect the integrity of the markup flow: function html(s) { return s.split('&').join('&amp;').split( '<').join('&lt;').split('>').join('&gt;').split('"').join('&quot;') }.Demurral
hmmh, I rolled my own. but now for chinese chars I got an encoding problem they look nice in javascript but not in xml :-( BTW: I didnt reply with at mojuba ...Fuzzy
github.com/karussell/jsii/blob/master/web/js/src/XmlHandler.jsFuzzy
(encoding problem fixed)Fuzzy
V
2

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.

Veridical answered 31/10, 2010 at 22:49 Comment(1)
thanks a lot! The only writer (not parser) is indeed: github.com/polotek/libxmljs/wiki or did I misread sth!?Fuzzy
T
3

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.

Turbellarian answered 15/7, 2014 at 7:53 Comment(0)
V
2

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.

Veridical answered 31/10, 2010 at 22:49 Comment(1)
thanks a lot! The only writer (not parser) is indeed: github.com/polotek/libxmljs/wiki or did I misread sth!?Fuzzy
Z
2

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.

Zina answered 8/2, 2011 at 17:42 Comment(0)
S
1

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)

Skeet answered 26/2, 2011 at 18:22 Comment(3)
how do you write the output to file?Dys
@KamalSalem. I assume you are referring to a web browser writing to the user's local disk, if so that's not possible.Skeet
@AlexanderN You could use the Data URI scheme to generate a file in the browserDuvalier
H
0

This lib for Node.js is very stable and easy to use: https://github.com/minchenkov/simple-xml-writer

Hyphenated answered 7/6, 2013 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.