Home >Web Front-end >JS Tutorial >How to use xml2js in nodej
This time I will show you how to use xml2js in nodej, what are the precautions when using xml2js in nodej, the following is a practical case, let's come together take a look.
Nodejs json and xml conversion tool ---xml2js
Download method
npm install xml2js
The example is as follows
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('----------');
The result is as follows
---------- {"root":"Hello xml2js!"} ----------<?xml version="1.0" encoding="UTF-8" standalone="yes"?><root> <name>Super</name> <Surname>Man</Surname> <age>23</age></root>----------
I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to the php Chinese websiteOther related articles!
Related reading:
How to make a bottom navigation TabBar on the vue homepage
One made with Vue.js 2.0+ Graphite document style rich text editor
The above is the detailed content of How to use xml2js in nodej. For more information, please follow other related articles on the PHP Chinese website!