XML
@office-open/xml
Zero-dependency XML parsing and serialization — drop-in replacement for xml + xml-js
Installation
pnpm add @office-open/xml
npm install @office-open/xml
yarn add @office-open/xml
bun add @office-open/xml
API
xml2js(xml, options?)
Parses an XML string into an Element tree.
import { xml2js } from "@office-open/xml";
const root = xml2js(`<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:r><w:t>Hello</w:t></w:r>
</w:p>`);
js2xml(root, options?)
Serializes an Element tree back into an XML string.
import { xml2js, js2xml } from "@office-open/xml";
const output = js2xml(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 |
attrMeasure(element, name, type?) | Measure attribute (keeps units) |
hasChild(parent, name) | Check if child element exists |
findDeep(parent, name) | Find all descendant elements |
findFirst(parent, name) | First descendant (short-circuit) |