npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

mm_xml

v1.1.7

Published

这是超级美眉xml序列化、反序列化帮助模块,适用于koa框架

Readme

mm_xml

一个强大的XML序列化和反序列化模块,专为Koa框架设计。支持XML和JSON之间的相互转换,并提供了便捷的中间件功能。适用于微信公众号消息开发等需要xml传参的场景

功能特点

  • XML转JSON对象
  • JSON对象转XML字符串
  • Koa中间件支持
  • 支持CDATA标签
  • 自动类型转换(数字、布尔值、日期等)
  • 支持数组类型
  • 错误处理机制

安装

npm install mm_xml

基本用法

作为Koa中间件使用

const Koa = require('koa');
const xml = require('mm_xml');

const app = new Koa();

// 使用默认配置
app.use(xml());

// 使用自定义配置
app.use(xml({
    key: 'body',        // 解析后的数据存储键名,默认为'body'
    limit: '4mb',       // 请求体大小限制
    encoding: 'utf8',   // 编码格式
    onerror: (err, ctx) => {  // 错误处理函数
        console.error(err);
    }
}));

XML字符串转JSON对象

// 示例XML字符串
const xml = `
<xml>
    <ToUserName><![CDATA[toUser]]></ToUserName>
    <FromUserName><![CDATA[fromUser]]></FromUserName>
    <CreateTime>12345678</CreateTime>
    <MsgType>text</MsgType>
    <Content><![CDATA[你好]]></Content>
</xml>
`;

// 转换为JSON对象
const jsonObj = xml.toXml();
console.log(jsonObj);

/* 输出结果:
{
    ToUserName: 'toUser',
    FromUserName: 'fromUser',
    CreateTime: 12345678,
    MsgType: 'text',
    Content: '你好'
}
*/

JSON对象转XML字符串

const toXml = require('mm_xml/xml-parser');

const obj = {
    ToUserName: 'toUser',
    FromUserName: 'fromUser',
    CreateTime: 12345678,
    MsgType: 'text',
    Content: '你好'
};

const xmlString = toXml(obj);
console.log(xmlString);

/* 输出结果:
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你好]]></Content>
*/

API参考

Koa中间件选项

| 参数 | 类型 | 默认值 | 说明 | |------|------|--------|------| | key | String | 'body' | 解析后的数据存储键名 | | limit | String | '4mb' | 请求体大小限制 | | encoding | String | 'utf8' | 编码格式 | | onerror | Function | - | 错误处理函数 |

类型转换规则

模块会自动进行以下类型转换:

  1. 数字类型:符合数字格式的值会被转换为Number
  2. 布尔类型:'true'和'false'会被转换为Boolean
  3. 日期类型:符合日期格式的字符串会被转换为Date对象
  4. 数组:重复的标签会被转换为数组
  5. 字符串:其他类型会保持字符串格式,并使用CDATA包装

特殊处理

  1. CDATA标签:

    • XML转JSON时会自动去除CDATA标签
    • JSON转XML时会自动为字符串类型添加CDATA标签
  2. 数组处理:

    • 支持多层嵌套的数组结构
    • 数组元素可以是简单类型或复杂对象

完整示例

require('mm_xml/xml-parser');

// 复杂XML示例
const xml = `
<xml>
    <ToUserName><![CDATA[toUser]]></ToUserName>
    <FromUserName><![CDATA[fromUser]]></FromUserName>
    <CreateTime>12345678</CreateTime>
    <MsgType>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>2019-09-26 00:00:00</CreateTime>
    </MsgType>
    <Content><![CDATA[你好]]></Content>
</xml>
`;

// 转换为JSON对象
const result = xml.toXml();
console.log(result);

/* 输出结果:
{
    ToUserName: 'toUser',
    FromUserName: 'fromUser',
    CreateTime: 12345678,
    MsgType: {
        FromUserName: 'fromUser',
        CreateTime: [Date对象]
    },
    Content: '你好'
}
*/

注意事项

  1. 中间件仅处理以下情况的请求:

    • HTTP方法为POST、PUT或PATCH
    • Content-Type包含'xml'
    • 请求体未被解析(ctx.request[bodyKey]未定义)
  2. 响应处理:

    • 当响应体为对象且Content-Type包含'xml'时,会自动将对象转换为XML格式
  3. 错误处理:

    • XML解析错误会抛出400状态码
    • 可通过onerror选项自定义错误处理

许可证

ISC License

作者

qww