10

April 10, 2026 · View on GitHub

@nodable/flexible-xml-parser ships complete TypeScript definitions for both ESM (src/fxp.d.ts) and CommonJS (lib/fxp.d.cts). No @types package is needed.


Basic Usage

import XMLParser, { X2jOptions } from '@nodable/flexible-xml-parser';

const options: X2jOptions = {
  skip:    { attributes: false, nsPrefix: true },
  nameFor: { cdata: '#cdata' },
  limits:  { maxNestedTags: 100 },
};

const parser = new XMLParser(options);
const result = parser.parse('<root><tag>42</tag></root>');

Key Exported Types

ExportDescription
XMLParserThe parser class (also the default export)
X2jOptionsFull options interface for new XMLParser(options)
ParseErrorError class thrown on parse failures
ErrorCodeConst object with all error code strings
ErrorCodeValueUnion type of all error code values
SkipOptionsType for the skip option group
NameForOptionsType for the nameFor option group
AttributeOptionsType for the attributes option group
TagOptionsType for the tags option group
DoctypeOptionsType for the doctypeOptions option group
LimitsOptionsType for the limits option group
FeedableOptionsType for the feedable option group
SkipTagEntryObject form of a skip.tags entry
StopNodeEntryObject form of a stopNodes entry
Enclosure{ open: string; close: string } pair
xmlEnclosuresBuilt-in XML enclosure array (comments + CDATA)
quoteEnclosuresBuilt-in quote enclosure array

Error Handling

import XMLParser, { ParseError, ErrorCode } from '@nodable/flexible-xml-parser';

const parser = new XMLParser({ limits: { maxNestedTags: 100 } });

try {
  parser.parse(xml);
} catch (e) {
  if (e instanceof ParseError) {
    // e.code is typed as ErrorCodeValue
    if (e.code === ErrorCode.LIMIT_MAX_NESTED_TAGS) {
      console.error('Document too deeply nested');
    } else {
      console.error(e.code, e.message, `line ${e.line} col ${e.col}`);
    }
  } else {
    throw e;
  }
}

Custom Output Builder

BaseOutputBuilder and ElementType are imported from @nodable/base-output-builder:

import { BaseOutputBuilder, ElementType } from '@nodable/base-output-builder';
import type { TagDetail, ReadOnlyMatcher } from '@nodable/base-output-builder';

class TagListBuilder extends BaseOutputBuilder {
  private tags: string[] = [];

  addElement(tag: TagDetail, matcher: ReadOnlyMatcher): void {
    this.tags.push(tag.name);
  }

  getOutput(): string[] {
    return this.tags;
  }
}

Custom Value Parser

import { ElementType } from '@nodable/base-output-builder';
import type { ValueParserContext } from '@nodable/base-output-builder';

class UpperCaseParser {
  parse(val: unknown, context?: ValueParserContext): unknown {
    if (context?.elementType === ElementType.ATTRIBUTE) return val;
    return typeof val === 'string' ? val.toUpperCase() : val;
  }
}

ESM vs CommonJS

The package uses "type": "module" with a bundled CJS output. TypeScript resolves the correct types automatically via the exports field in package.json:

  • ESM (import): resolves to src/fxp.d.ts
  • CJS (require): resolves to lib/fxp.d.cts

No extra tsconfig configuration is needed for standard setups.


This is the end of the documentation series.

← Back to README