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 🙏

© 2026 – Pkg Stats / Ryan Hefner

nml-ex

v1.0.0

Published

JSON <=> XML

Downloads

123

Readme

nml-ex

nml-ex是在NML基础上进行的扩充,NML 是 JSON 的扩展,用于实现 JSON 与 XML 之间的双向转换。它基于 JSON 语法(Standard ECMA-262 3rd Edition - December 1999), 通过 Value 类型为 JSON 节点附加 XML 属性。

语法定义

nml
    { member }

object
    {}
    { member (, member)* }

member
    string : value

value
    string | number | boolean | null | object | array | Value

array
    []
    [ element (, element)* ]

element
    value

Value(v, attrs)
    { "attrs" : attributes, "value" : v }

attributes
    {}
    { pair (, pair)* }

pair
    string : string

安装

npm install nml-ex

使用

const nml = require('nml-ex');
const Value = require('nml-ex/Value');

解析 XML → JSON

const xml = '<name sex="0" age="14">May</name>';
const json = nml.parse(xml);
// → { name: Value({ sex: '0', age: '14' }, 'May') }

有属性的节点解析为 Value 对象,attrs 存放属性,value 存放文本内容。 无属性的节点直接返回其值,Value 包装会被自动剥离。

序列化 JSON → XML

const obj = { name: new Value({ sex: '0', age: '14' }, 'May') };
nml.stringify(obj);
// → <?xml version="1.0" encoding="UTF-8"?>
//   <name sex="0" age="14">May</name>

// 带 XSL 样式表
nml.stringify(obj, 'transform.xsl');

数组

同名元素自动合并为数组:

const xml = '<root><item>a</item><item>b</item></root>';
nml.parse(xml);
// → { root: { item: ['a', 'b'] } }

nml.stringify({ root: { item: ['a', 'b'] } });
// → <root><item>a</item><item>b</item></root>

[join] — 混合内容

当数组的键名为 [join] 时,子元素直接拼接而不包裹标签, 适用于 XML 混合内容(文本与元素交错):

const obj = {
    root: {
        '[join]': ['Hello ', new Value({}, { b: 'World' }), '!'],
    },
};
nml.stringify(obj);
// → <root>Hello <b>World</b>!</root>

[join] 中的 nullundefined 会被静默跳过。

null / undefined / 空值

| 值 | XML 输出 | |----|---------| | null | <tag/> | | undefined | <tag/> | | "" (空字符串) | <tag></tag> | | 0 / false | <tag>0</tag> / <tag>false</tag> |

注释与 CDATA

解析时自动跳过 XML 注释,提取 CDATA 原文:

const xml = '<?xml version="1.0"?><root><!-- 注释 --><msg><![CDATA[<raw>文本</raw>]]></msg></root>';
nml.parse(xml);
// → { root: { msg: '<raw>文本</raw>' } }

CDATA 中的内容不做实体反转义,保留原始文本。

Value API

const Value = require('nml-ex/Value');

构造函数

new Value(attrs, value)
  • attrs — 属性对象 { key: 'value', ... }
  • value — 节点值,可以是任意 NML 支持的类型

静态方法

| 方法 | 说明 | |------|------| | Value.escape(str) | 转义 XML 特殊字符 & < > " ' | | Value.unescape(str) | 反转义 XML 实体 | | Value.isEmpty(o) | 判断对象/值是否为空 | | Value.stringify(attrs) | 将属性对象序列化为 XML 属性字符串 | | Value.parse(str) | 从 XML 属性字符串解析为属性对象 | | Value.compare(o1, o2) | 深度比较两个值是否相等 |

类型说明

XML 文本节点不携带类型信息,解析时所有简单值均返回字符串。 例如 <count>0</count> 解析为 { count: "0" }(字符串),而非数字 0

License

MIT © mityburner