coa-xml
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

coa-xml

GitHub license npm version npm downloads PRs Welcome

一个简单的 XML 和 JS 对象转换库。基于 xml2js 做简单封装

特点

根据日常实际项目使用情况:

  • 整理并内置了一些默认参数,满足绝大多数使用场景
  • 统一了异步表现形式,全部返回 Promise
  • 内置类型引用,无需额外查看文档,开箱即用,IDE 友好

快速开始

安装

yarn add coa-xml

直接使用

import { xml } from 'coa-xml'

// 将JS对象转换成xml字符串
await xml.encode({ name: 'A', gender: 1 }) // <xml><name>A</name><gender>1</gender></xml>

// 将xml字符串转为JS对象
await xml.encode('<xml><name>A</name><gender>1</gender></xml>') // { name: 'A', gender: 1 }

自定义配置

创建一个自定义实例,该实例的用法和xml对象完全一致

import { CoaXml } from 'coa-xml'

// 根据自定义配置创建实例
const xml = new CoaXml({ cdata: false })

// 将JS对象转换成xml字符串
await xml.encode({ name: 'A', gender: 1 }) // <xml><name>A</name><gender>1</gender></xml>

// 将xml字符串转为JS对象
await xml.encode('<xml><name>A</name><gender>1</gender></xml>') // { name: 'A', gender: 1 }

其中,默认配置为

const DefaultOptions = {
  rootName: 'xml',
  explicitArray: false,
  cdata: true,
  headless: true,
  explicitRoot: false,
}

更多配置的说明详见 xml2js 原文

Readme

Keywords

Package Sidebar

Install

npm i coa-xml

Weekly Downloads

7

Version

1.1.1

License

MIT

Unpacked Size

5.11 kB

Total Files

9

Last publish

Collaborators

  • adaex