# @file-type/xml 中文文档

规范地址：https://npmcn.com/package/%40file-type/xml/

版本：1\.2\.0
npm 元数据更新时间：2026-10-24T11:54:32.144Z
中文译文更新时间：2026-11-14T17:37:54.906Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40file-type/xml
原文对照：https://npmcn.com/package/%40file-type/xml/?lang=original
项目主页：https://github.com/Borewit/file-type-xml#readme
源码仓库：https://github.com/Borewit/file-type-xml

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：21.2 kB，7 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2024-12-17T23:33:16.684Z
- 最新版本发布：2026-10-05T17:14:55.823Z
- 近 30 天下载量：877833（2026-10-15 至 2026-11-13，npm 官方下载统计）
- 已发布版本数：15（近 12 个月 5 个）

## 中文简介

文件类型的 XML 检测插件

## 安装

```sh
npm install @file-type/xml
```

也可以使用：`pnpm add @file-type/xml`、`yarn add @file-type/xml`、`bun add @file-type/xml`

## README

[![NPM version](https://img.shields.io/npm/v/@file-type/xml.svg)](https://npmjs.org/package/@file-type/xml) [![Node.js CI](https://github.com/Borewit/file-type-xml/actions/workflows/nodejs-ci.yml/badge.svg)](https://github.com/Borewit/file-type-xml/actions/workflows/nodejs-ci.yml) [![npm downloads](http://img.shields.io/npm/dm/@file-type/xml.svg)](https://npmcharts.com/compare/@file-type/xml?start=365)

# @ 文件类型/ xml

探测器插件 [文件类型](https://github.com/sindresorhus/file-type) 用于 XML 文件.

## 安装

```bash
npm install @file-type/xml
```

## 使用量

以下示例显示如何将 XML 检测器添加到 [文件类型](https://github.com/sindresorhus/file-type)。 。 。 .

```js
import {FileTypeParser} from 'file-type';
import {detectXml} from '@file-type/xml';

const parser = new FileTypeParser({customDetectors: [detectXml]});
const fileType = await parser.fromFile('example.kml');
console.log(fileType);
```

与 `file-type` 22,节点js 22或更新是需要的. 对流道检测,通过 a 网络 `ReadableStream` 改为 `parser.fromStream()`转换节点 `Readable` 与 `Readable.toWeb()`数字 :

```js
import {createReadStream} from 'node:fs';
import {Readable} from 'node:stream';

const fileType = await parser.fromStream(Readable.toWeb(createReadStream('example.kml')));
```

探测器最多检查了16个KiB,包括任何字节顺序标记. 为了改变这个 限制,创建带有 `sampleSize` 以字节表示 :

```js
import {FileTypeParser} from 'file-type';
import {createXmlDetector} from '@file-type/xml';

const parser = new FileTypeParser({
    customDetectors: [createXmlDetector({sampleSize: 64 * 1024})]
});
```

限制既适用于Prolog检测,也适用于XML解析. 如果没有 XML 签名,则 在样本中找到, 检测器返回 `undefined` 而不消耗投入. 如果XML得到承认,但其具体格式无法在样本中确定, 它会返回 `application/xml`。 。 。 . `sampleSize` 必须是正的安全整数. 这个 `file-type` 流采样选项是分开的; 当使用时 `fileTypeStream`设置 联合国 `sampleSize` 如果你想要一个更大的样本.

您也可以使用外部的 XML 探测器 [文件类型](https://github.com/sindresorhus/file-type)数字 :

```js
import {XmlTextDetector} from '@file-type/xml';

const xmlTextDetector = new XmlTextDetector();
xmlTextDetector.write('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
const fileType = xmlTextDetector.fileType;
console.log(JSON.stringify(fileType)); // Outputs: {"ext":"svg","mime":"image/svg+xml"}
```

## 支持文件格式

-   [XML 数据](https://en.wikipedia.org/wiki/XML) (XML 的默认, 除非检测到更具体的格式)
-   [苹果属性列表, `.plist`](https://en.wikipedia.org/wiki/Property_list)
-   [原子](https://en.wikipedia.org/wiki/Atom_\(web_standard\))
-   [多库](https://en.wikipedia.org/wiki/DocBook)
-   [GML (地理标记语言)](https://en.wikipedia.org/wiki/Geography_Markup_Language)
-   [GPX( GPS 交换格式)](https://en.wikipedia.org/wiki/GPS_Exchange_Format)
-   [KML (关键孔标记语言)](https://en.wikipedia.org/wiki/Keyhole_Markup_Language)
-   [MathML(数学标记语言)](https://en.wikipedia.org/wiki/MathML)
-   [音乐XML, 未压缩](https://en.wikipedia.org/wiki/MusicXML)
-   [OPML( 大纲处理器标记语言)](https://en.wikipedia.org/wiki/OPML)
-   [RSS( RDF 站点摘要或非常简单的聚合)](https://en.wikipedia.org/wiki/RSS)
-   [SMIL:(同步多媒体融合语言)](https://en.wikipedia.org/wiki/Synchronized_Multimedia_Integration_Language)
-   [SVG:(可缩放矢量图形)](https://en.wikipedia.org/wiki/SVG)
-   [TEI, 文本编码倡议](https://en.wikipedia.org/wiki/Text_Encoding_Initiative)
-   [TTML : (时间文本标记语言)](https://en.wikipedia.org/wiki/Timed_Text_Markup_Language)
-   [X3D (可扩展的 三维)](https://en.wikipedia.org/wiki/X3D)
-   [第XHTML号](https://en.wikipedia.org/wiki/XHTML)
-   [XLIFF (XML 本地化 互换文件格式)](https://en.wikipedia.org/wiki/XLIFF)

## 许可证

本项目根据以下规定获得许可证: [MIT 许可证](https://npmcn.com/package/%40file-type/xml/LICENSE.txt). 随意使用、修改和按需要分发.

## 常见问题

### @file-type/xml 是什么？

文件类型的 XML 检测插件

### 如何安装 @file-type/xml？

在项目目录运行 npm install @file-type/xml，或使用 pnpm add / yarn add / bun add。

### @file-type/xml 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @file-type/xml 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @file-type/xml 可以商用吗？

@file-type/xml 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @file-type/xml 的下载量有多少？

根据 npm 官方下载统计，@file-type/xml 在 2026/10/15 – 2026/11/13 这 30 天内被下载 877,833 次，日均约 29,261 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @file-type/xml 还在维护吗？

最新版本 1\.2\.0 发布于 2026/10/06（1 个月前）；至今共发布 15 个版本，其中过去 12 个月发布了 5 个；首次发布于 2024/12/18。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

