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!?
- / (LGPL)
- not yet released: (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');
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');
Share
Improve this question
edited Oct 31, 2010 at 22:07
Karussell
asked Oct 31, 2010 at 22:02
KarussellKarussell
17.4k17 gold badges100 silver badges203 bronze badges
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. – mojuba Commented Oct 31, 2010 at 22:16
- But I cannot imagine that there isn't a single lib which already does this ... exactly that is the problem: "angle brackets etc" :-) – Karussell Commented Oct 31, 2010 at 22:29
- @Karussell: (please do reply with @mojuba 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("&").split( "<").join("<").split(">").join(">") } (sorry, formatting is not available here) – mojuba Commented Oct 31, 2010 at 22:40
- 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('&').split( '<').join('<').split('>').join('>').split('"').join('"') }. – mojuba Commented Oct 31, 2010 at 23:01
- 1 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 ... – Karussell Commented Nov 1, 2010 at 0:21
5 Answers
Reset to default 3I'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.
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.
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.
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)
This lib for Node.js is very stable and easy to use: https://github.com/minchenkov/simple-xml-writer