https://github.com/vilien/xmlstring2json
xml字符串转换json格式,适用于微信小程序
https://github.com/vilien/xmlstring2json
json wechat-applet xml
Last synced: 4 months ago
JSON representation
xml字符串转换json格式,适用于微信小程序
- Host: GitHub
- URL: https://github.com/vilien/xmlstring2json
- Owner: vilien
- License: other
- Created: 2017-02-08T09:47:30.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-26T03:51:28.000Z (almost 9 years ago)
- Last Synced: 2026-01-23T19:56:42.801Z (5 months ago)
- Topics: json, wechat-applet, xml
- Language: JavaScript
- Size: 9.77 KB
- Stars: 22
- Watchers: 1
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# xml字符串转换json格式
可用于微信小程序xml字符串转换json格式
## 安装
### npm方式安装
```
npm install --save xmlstring2json
```
### bower方式安装
```
bower install xmlstring2json
```
## 用法示例:
bookstore.xml文件
```xml
Harry Potter
J K. Rowling
2005
29.99
Learning XML
Erik T. Ray
2003
39.95
```
### require方式
```javascript
var fs = require('fs');
var xml2json = require('xmlstring2json');
var xml = fs.readFileSync('./test/bookstore.xml', 'utf8');
console.log(JSON.stringify(xml2json(xml), null, 4));
```
### 引入方式
```html
$.get({
url: './test/bookstore.xml',
dataType: 'text',
success: function(xml){
console.log(JSON.stringify(xml2json(xml), null, 4));
}
});
```
### 输出
```javascript
{
"bookstore": {
"book": [
{
"@category": "CHILDREN",
"title": {
"#text": "Harry Potter"
},
"author": {
"#text": "J K. Rowling"
},
"year": {
"#text": "2005"
},
"price": {
"#text": "29.99"
}
},
{
"@category": "WEB",
"title": {
"#text": "Learning XML"
},
"author": {
"#text": "Erik T. Ray"
},
"year": {
"#text": "2003"
},
"price": {
"#text": "39.95"
}
}
]
}
}
```
## 注意
该模块暂不检测xml格式合法性,即认为输入的xml都是合法的。如格式不合法,可能出现输出为空的情况。