开发者

Libraries to write xml with JavaScript

I am doing some server side c开发者_开发问答oding 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');


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜