https://github.com/coajs/coa-xml
一个简单的XML和JS对象转换库
https://github.com/coajs/coa-xml
coa coa-xml coajs xml xml2js
Last synced: 6 months ago
JSON representation
一个简单的XML和JS对象转换库
- Host: GitHub
- URL: https://github.com/coajs/coa-xml
- Owner: coajs
- License: mit
- Created: 2020-05-22T17:00:53.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-06-29T11:26:59.000Z (over 2 years ago)
- Last Synced: 2025-06-06T03:02:41.806Z (7 months ago)
- Topics: coa, coa-xml, coajs, xml, xml2js
- Language: TypeScript
- Homepage: https://npmjs.org/coa-xml
- Size: 118 KB
- Stars: 0
- Watchers: 0
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# coa-xml
[](LICENSE)
[](https://www.npmjs.org/package/coa-xml)
[](http://npm-stat.com/charts.html?package=coa-xml)
[](https://github.com/coajs/coa-xml/pulls)
一个简单的 XML 和 JS 对象转换库。基于 [xml2js](https://www.npmjs.com/package/xml2js) 做简单封装
## 特点
根据日常实际项目使用情况:
- 整理并内置了一些默认参数,满足绝大多数使用场景
- 统一了异步表现形式,全部返回 Promise
- 内置类型引用,无需额外查看文档,开箱即用,IDE 友好
## 快速开始
### 安装
```shell
yarn add coa-xml
```
### 直接使用
```typescript
import { xml } from 'coa-xml'
// 将JS对象转换成xml字符串
await xml.encode({ name: 'A', gender: 1 }) // A1
// 将xml字符串转为JS对象
await xml.decode('A1') // { name: 'A', gender: 1 }
```
### 自定义配置
创建一个自定义实例,该实例的用法和`xml`对象完全一致
```typescript
import { CoaXml } from 'coa-xml'
// 根据自定义配置创建实例
const xml = new CoaXml({ cdata: false })
// 将JS对象转换成xml字符串
await xml.encode({ name: 'A', gender: 1 }) // A1
// 将xml字符串转为JS对象
await xml.decode('A1') // { name: 'A', gender: 1 }
```
其中,默认配置为
```typescript
const DefaultOptions = {
rootName: 'xml',
explicitArray: false,
cdata: true,
headless: true,
explicitRoot: false,
}
```
更多配置的说明详见 [xml2js](https://www.npmjs.com/package/xml2js#options) 原文