XML
@office-open/xml
面向 Office Open XML 文档的 XML 解析和序列化库
安装
pnpm add @office-open/xml
API
parse(xml, options?)
将 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>`);
stringify(root, options?)
将 Element 树序列化回 XML 字符串。
import { parse, stringify } from "@office-open/xml";
const output = stringify(root);
查询工具
| 函数 | 说明 |
|---|---|
findChild(parent, name) | 第一个匹配标签名的直接子元素 |
children(parent, name) | 所有匹配标签名的直接子元素 |
childText(parent, name) | 第一个匹配子元素的文本内容 |
attr(element, name) | 属性值(字符串) |
attrNum(element, name) | 属性值(数字) |
hasChild(parent, name) | 检查子元素是否存在 |
findDeep(parent, name) | 查找所有后代元素 |