这次给大家带来nodej中的xml2js需要如何使用,使用nodej中的xml2js的注意事项有哪些,下面就是实战案例,一起来看一下。
nodejs json与xml相互转化的工具---xml2js
下载方法
npm install xml2js
实例如下
var xml2js = require('xml2js'); //xml->json
//xml2js默认会把子子节点的值变为一个数组, explicitarray设置为false
var xmlparser = new xml2js.parser({explicitarray : false, ignoreattrs : true}) //json->xml
var jsonbuilder = new xml2js.builder(); //测试用例
var xml = "<root>hello xml2js!</root>"; var obj = {name: "super", surname: "man", age: 23}; console.log('----------');
// xml -> json
xmlparser.parsestring(xml, function (err, result) { //将返回的结果再次格式化
console.log(json.stringify(result));
}); console.log('----------'); //json --> xml
var builder = new xml2js.builder(); var jsonxml = builder.buildobject(obj); console.log(jsonxml); console.log('----------');
结果如下
----------
{"root":"hello xml2js!"}
----------<?xml version="1.0" encoding="utf-8" standalone="yes"?><root>
<name>super</name>
<surname>man</surname>
<age>23</age></root>----------
相信看了这些案例你已经掌握了方法,更多精彩请关注其它相关文章!
相关阅读:
在vue首页怎样做出底部导航tabbar
一个用vue.js 2.0+做出的石墨文档样式的富文本编辑器
以上就是nodej中的xml2js需要如何使用的详细内容。