Skip to content

Repository files navigation

@jetstreamapp/simple-xml

A minimal, zero-dependency XML parser and builder. Built as a lightweight replacement for fast-xml-parser when you only need simple XML parsing and building — no DTD, no schema validation, no streaming, no complex entity processing.

100% browser compatible. No Node.js APIs.

Built with Claude Code

Why?

  • Zero dependencies — nothing to audit, nothing to break
  • Tiny footprint — ~300 lines of code total
  • Browser compatible — works anywhere JavaScript runs
  • Fast-xml-parser compatible output — same object shape, drop-in replacement for simple use cases

Installation

npm install @jetstreamapp/simple-xml

Quick Start

import{parse,build}from'@jetstreamapp/simple-xml';// Parse XML to a JavaScript objectconstresult=parse('<root><name>hello</name><count>42</count></root>');// { root: { name: 'hello', count: 42 }}// Build XML from a JavaScript objectconstxml=build({root: {name: 'hello',count: 42}});// '<root><name>hello</name><count>42</count></root>'

API

parse(xml: string, options?: ParseOptions): Record<string, unknown>

Parses an XML string into a JavaScript object.

constresult=parse(soapResponse,{trimValues: true,ignoreAttributes: false,removeNSPrefix: true,attributeNamePrefix: '@_',processEntities: false,});

ParseOptions

OptionTypeDefaultDescription
trimValuesbooleantrueTrim whitespace from text content
ignoreAttributesbooleanfalseSkip attributes entirely
removeNSPrefixbooleanfalseStrip namespace prefixes (soap:BodyBody)
attributeNamePrefixstring'@_'Prefix prepended to attribute names in the output object
parseTagValuebooleantrueCoerce numeric/boolean text values to their JS types
processEntitiesbooleantrueDecode XML entities (&amp;&, &lt;<, etc.)
maxDepthnumber256Maximum nesting depth allowed. Throws if exceeded
strictbooleanfalseThrow on malformed XML (unclosed, mismatched, or extra closing tags)

build(obj: Record<string, unknown>, options?: BuildOptions): string

Builds an XML string from a JavaScript object.

constxml=build({Package: {'@xmlns': 'http://soap.sforce.com/2006/04/metadata',types: [{members: ['MyClass','OtherClass'],name: 'ApexClass'}],version: '50.0',},},{format: true,ignoreAttributes: false,attributeNamePrefix: '@'},);

BuildOptions

OptionTypeDefaultDescription
formatbooleanfalsePretty-print with indentation
ignoreAttributesbooleanfalseSkip attributes when building XML
attributeNamePrefixstring'@_'Prefix used to identify attribute keys in the input object
indentBystring' 'Indentation string used when format is true

Output Shape

The parser produces the same object shape as fast-xml-parser:

  • Single child element → value directly on parent: { parent: { child: 'value' } }
  • Multiple same-name children → array: { parent: { item: ['a', 'b', 'c'] } }
  • Attributes → prefixed keys: { element: { '@_id': '1', '#text': 'content' } }
  • Text-only leaf → string/number/boolean value: { element: 'hello' }
  • Empty/self-closing → empty string: { element: '' }

Supported XML Features

  • Elements, attributes, text content
  • Namespace prefix stripping
  • CDATA sections
  • Comments (skipped)
  • Processing instructions (skipped)
  • Self-closing tags
  • XML declaration (skipped)
  • XML entities: named (&amp;, &lt;, &gt;, &quot;, &apos;) and numeric (&#123;, &#x7B;)

License

MIT

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages