Node.js 使用xml2js处理xml
2018-01-25 18:45
441 查看
(Mac环境)
https://github.com/Leonidas-from-XIV/node-xml2js
Android资源文件strings.xml
test.js
attrkey (default: $): Prefix that is used to access the attributes. Version 0.1 default was @.
charkey (default: _): Prefix that is used to access the character content. Version 0.1 default was #.
详细用法可到上述github查看
运行结果
https://github.com/Leonidas-from-XIV/node-xml2js
Mac 没有ll命令 $vim ~/.bash_profile //编辑文件 I //插入 alias ll='ls -alF' //取别名 wq //写入退出 $source ~/.bash_profile //使其生效,可以使用ll命令了
$npm install -g xml2js // 安装xml2js全局模块
$npm ls -g //查看全局模块安装依赖树
解决require("xml2js")全局模块找不到的问题: $vim ~/.bash_profile export NODE_PATH="/usr/local/lib/node_modules" //添加环境变量 $source ~/.bash_profile
Android资源文件strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="test1">aaa</string> <string name="test2">bbb</string> <string name="test3">ccc</string> </resources>
test.js
var fs = require('fs'); var xml2js = require('xml2js'); var parser = new xml2js.Parser(); var filePath = "./strings.xml"; fs.readFile(filePath, function(err, data) { parser.parseString(data, function (err, result) { var strings = result.resources.string for(var i=0;i< strings.length;i++ ){ console.log(strings[i].$.name) console.log(strings[i]._) } }); });
attrkey (default: $): Prefix that is used to access the attributes. Version 0.1 default was @.
charkey (default: _): Prefix that is used to access the character content. Version 0.1 default was #.
详细用法可到上述github查看
运行结果
$node test.js test1 aaa test2 bbb test3 ccc
相关文章推荐
- Node.js使用iis管理器中session丢失处理
- 解析Node.js异常处理中domain模块的使用方法
- NodeJS on Nginx: 使用nginx反向代理处理静态页面
- 解析Node.js异常处理中domain模块的使用方法
- 使用Node.js处理前端代码文件的编码问题
- node.js使用fs的文件处理注意事项
- Node.js权威指南 (5) - 使用Buffer类处理二进制数据
- 使用Raygun对Node.js应用进行错误处理的方法
- Node.js使用Buffer类处理二进制数据(二)
- 使用node.js中的Buffer类处理二进制数据的方法
- 使用 js 处理XML 文档
- NodeJS on Nginx: 使用nginx反向代理处理静态页面
- Node.js——使用Buffer类处理二进制数据
- Node.js 使用gm处理图像
- 使用Raygun对Node.js应用进行错误处理的方法
- node.js 使用-事件处理(EventEmitter)
- Node.js中处理异步编程(使用回调处理一次性事件,使用事件监听器处理重复性事件)
- 【转】NodeJS on Nginx: 使用nginx反向代理处理静态页面
- Node.js使用Buffer类处理二进制数据(一)
- 使用Node.js处理前端代码文件的编码问题