使用 JavaScript 编写 xml 的库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4065365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Libraries to write xml with JavaScript
提问by Karussell
I am doing some server side coding with JavaScript (node.js) and I would like to write valid xml.
我正在使用 JavaScript (node.js) 进行一些服务器端编码,并且我想编写有效的 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
如果项目快速、小巧且易于使用(按此顺序),那就太酷了。我想有一个较低级别的访问 ala
doc.addElement('xy').addAttr('name', 'bob');
采纳答案by alunny
There are a number of XML libraries for node.js listed at http://github.com/ry/node/wiki/modules#parsers-xml
http://github.com/ry/node/wiki/modules#parsers-xml列出了许多 node.js 的 XML 库
If memory serves, the one that has the most traction is http://github.com/polotek/libxmljs, which appears to be MIT licensed.
如果没记错的话,最具吸引力的是http://github.com/polotek/libxmljs,它似乎是 MIT 许可的。
回答by Wahid Kadwaikar
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.
并且,要将 XML 写入本地文件,请调用以下函数。
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 XMLWriterfor creating XML in memory.
我希望这会有所帮助,否则您可以尝试使用Ariel Flesler 的 XMLWriter在内存中创建 XML。
回答by Wes
I recently released node-genx, a wrapper around a small C-library called Genxthat provides fast and valid xml generation in node.js.
我最近发布了node-genx,它是一个名为Genx的小型 C 库的包装器,可在 node.js 中提供快速有效的 xml 生成。
Installation is simply:
安装很简单:
npm install genx
I have posted some examples of using itto generate an Atom feed and a Sphinx xmlpipe2stream on my blog.
我在我的博客上发布了一些使用它生成 Atom 提要和Sphinx xmlpipe2流的示例。
回答by Alex Nolasco
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
我发现 Ariel Flesler 的 XMLWriter 构造函数是从头(在内存中)创建 XML 的良好开端,看看这个
http://flesler.blogspot.com/2008/03/xmlwriter-for-javascript.html
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)
有几个警告,它不会转义字符串,并且语法会使coyote++变得丑陋。您可以从作者的站点或从https://github.com/alexandern/XMLWriter下载它 (包括对独立属性的转义和错误修复)
回答by Paulo Oliveira
This lib for Node.js is very stable and easy to use: https://github.com/minchenkov/simple-xml-writer
这个用于 Node.js 的 lib 非常稳定且易于使用:https: //github.com/minchenkov/simple-xml-writer

