Home > Web Front-end > JS Tutorial > body text

How to use xml2js in nodej

php中世界最好的语言
Release: 2018-03-07 10:30:16
Original
3723 people have browsed it


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
Copy after login

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(&#39;----------&#39;);  
  // xml -> json
  xmlParser.parseString(xml, function (err, result) {    //将返回的结果再次格式化
     console.log(JSON.stringify(result));
  });  console.log(&#39;----------&#39;);  //json --> xml
  var builder = new xml2js.Builder();  var jsonxml = builder.buildObject(obj);  console.log(jsonxml);  console.log(&#39;----------&#39;);
Copy after login

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>----------
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!