XML
解析
将 XML 字符串解析为 Element 树,支持可配置选项
基本解析
import { parse } from "@office-open/xml";
const root = parse(`<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:r><w:t>Hello</w:t></w:r>
</w:p>`);
// root.name === "w:p"
// root.elements[0].name === "w:r"
parse 函数将 XML 字符串转换为遵循 xml-js 格式的 Element 树。
parse(xml, options?)
const root = parse(xmlString, {
trim: true,
ignoreComment: true,
});
选项
| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
trim | boolean | false | 去除文本节点中的空白 |
ignoreDeclaration | boolean | false | 跳过 XML 声明(<?xml ...?>) |
ignoreComment | boolean | false | 跳过 XML 注释(<!-- -->) |
ignoreCdata | boolean | false | 跳过 CDATA 部分 |
ignoreDoctype | boolean | false | 跳过 DOCTYPE 声明 |
ignoreText | boolean | false | 跳过文本节点 |
nativeTypeAttributes | boolean | false | 将属性值转换为原生类型 |
Element 结构
每个解析后的节点是一个 Element 对象:
interface Element {
type: "element";
name: string;
attributes?: Record<string, string>;
elements?: Element[];
}
文本节点具有不同的结构:
// 文本元素
{ type: "text", text: "Hello World" }
命名空间处理
命名空间作为普通属性保留:
const root = parse(`<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:r><w:t>Hello</w:t></w:r>
</w:p>`);
// root.attributes["xmlns:w"] === "http://schemas.openxmlformats.org/..."
所有元素名称包含前缀,因此查询时使用 "w:p"、"w:r" 等。
xml2js / xml2json
为了 xml-js 兼容性,提供了别名:
import { xml2js, xml2json } from "@office-open/xml";
const element = xml2js(xmlString);
const jsonString = xml2json(xmlString);
从 ZIP 归档中读取
结合 @office-open/core 从 OOXML 文件中解析 XML:
import { readFileSync } from "node:fs";
import { unzipToMap, readXmlFromZip } from "@office-open/core";
const zip = unzipToMap(readFileSync("document.docx"));
const document = readXmlFromZip(zip, "word/document.xml");
// document 已经是 Element 树