XML
@office-open/xml
XML parsing and serialization library for Office Open XML documents
Installation
pnpm add @office-open/xml
API
parse(xml, options?)
Parses an XML string into an Element tree.
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?)
Serializes an Element tree back into an XML string.
import { parse, stringify } from "@office-open/xml";
const output = stringify(root);
Query Utilities
| Function | Description |
|---|---|
findChild(parent, name) | First direct child with tag name |
children(parent, name) | All direct children matching tag name |
childText(parent, name) | Text content of first matching child |
attr(element, name) | Attribute value as string |
attrNum(element, name) | Attribute value as number |
hasChild(parent, name) | Check if child element exists |
findDeep(parent, name) | Find all descendant elements |