XML parser using Synth's universal AST
npm install @sylphx/synth-xmlXML parser using Synth's universal AST. Conversion layer over fast-xml-parser.
- ✅ Strategic Dependency - Uses fast-xml-parser (battle-tested, high-performance)
- 🚀 Full XML 1.0 Support - Elements, attributes, text, comments, CDATA
- 🎯 Universal AST - Converts XML to Synth's language-agnostic format
- 🔌 Plugin System - Transform AST with sync/async plugins
- 📦 Production Ready - fast-xml-parser used in thousands of projects
``bash`
npm install @sylphx/synth-xml
`typescript
import { parse } from '@sylphx/synth-xml'
const xml =
const tree = parse(xml)
console.log(tree.nodes[tree.root])
`
`typescript
import { XMLParser, createParser, parse, parseAsync } from '@sylphx/synth-xml'
// Standalone function (recommended)
const tree = parse('
// Async parsing (for plugins)
const tree = await parseAsync('
// Class instance
const parser = new XMLParser()
const tree = parser.parse('
// Factory function
const parser = createParser()
const tree = parser.parse('
`
`typescript
import { parse } from '@sylphx/synth-xml'
// Ignore attributes
const tree = parse(xml, { ignoreAttributes: true })
// Remove namespace prefixes
const tree = parse(xml, { removeNSPrefix: true })
// Parse attribute values as numbers/booleans
const tree = parse(xml, { parseAttributeValue: true })
// Trim whitespace from values
const tree = parse(xml, { trimValues: true })
`
`typescript
import { parse, type Tree } from '@sylphx/synth-xml'
// Sync plugin
const myPlugin = {
name: 'my-plugin',
transform(tree: Tree) {
// Modify tree
return tree
}
}
const tree = parse(xmlSource, { plugins: [myPlugin] })
// Async plugin
const asyncPlugin = {
name: 'async-plugin',
async transform(tree: Tree) {
// Async modifications
return tree
}
}
const tree = await parseAsync(xmlSource, { plugins: [asyncPlugin] })
`
The parser generates a universal Synth AST by converting fast-xml-parser's output. Each node includes:
`typescript`
{
type: 'Element' | 'Text' | 'Comment' | 'CDATA',
parent: NodeId,
children: [NodeId],
span: {
start: { offset, line, column },
end: { offset, line, column }
},
data: {
tagName?: string, // For Element nodes
attributes?: object, // For Element nodes
text?: string // For Text/Comment/CDATA nodes
}
}
- ✅ Nested elements
- ✅ Empty elements$3
- ✅ Single and multiple attributes
- ✅ Double and single quotes
- ✅ Boolean attributes
- ✅ Numeric attributes$3
- ✅ Text content
- ✅ Mixed content (text + elements)
- ✅ CDATA sections
- ✅ Comments
- ✅ Multiline content$3
- ✅ Default namespaces
- ✅ Prefixed namespaces
- ✅ Multiple namespaces
- ✅ Namespace declarations$3
- ✅ Entity references (<, >, &, ", ')
- ✅ Numeric character references
- ✅ Escaped characters$3
- ✅ Processing instructions
- ✅ XML declarations
- ✅ Deep nesting
- ✅ Whitespace handlingExamples
$3
`typescript
const xml = const tree = parse(xml)
`$3
`typescript
const xml = const tree = parse(xml)
`$3
`typescript
const xml = const tree = parse(xml)
`$3
`typescript
const xml = const tree = parse(xml)
`$3
`typescript
const xml = const tree = parse(xml)
`$3
`typescript
const xml = const tree = parse(xml)
`$3
`typescript
const xml = const tree = parse(xml)
`$3
`typescript
const xml = const tree = parse(xml)
`$3
`typescript
const xml = const tree = parse(xml)
`$3
`typescript
const xml = const tree = parse(xml)
`Performance
Leverages fast-xml-parser's proven performance:
- Extremely fast parsing (up to 10x faster than alternatives)
- Low memory footprint
- Support for large XML files
- Efficient streaming support
Development Philosophy
This package uses a strategic dependency approach:
- Third-party parser: fast-xml-parser (battle-tested, high-performance)
- Our conversion layer: fast-xml-parser output → Synth universal AST
- Our value: Universal format, cross-language tools, plugin system
$3
- ❌ Writing XML parser: 100+ hours, complex spec, edge cases
- ✅ Using fast-xml-parser: Battle-tested, high-performance, actively maintained
- Our focus: Universal AST format, transformations, cross-language operations
Use Cases
- Configuration parsing: Parse XML config files
- RSS/Atom feeds: Parse and analyze feeds
- SVG manipulation: Parse and transform SVG files
- SOAP services: Parse SOAP messages
- Build systems: Parse Maven POM, Ant, MSBuild files
- Android development: Parse Android manifests
- Document analysis: Extract content from XML documents
- Cross-language tools: Analyze XML + JavaScript + Python together
Options Reference
`typescript
interface XMLParseOptions {
// Ignore all attributes
ignoreAttributes?: boolean // Remove namespace prefix from tag names
removeNSPrefix?: boolean
// Parse attribute values as numbers/booleans
parseAttributeValue?: boolean
// Parse tag values as numbers/booleans
parseTagValue?: boolean
// Trim whitespace from values
trimValues?: boolean
// Plugin system
plugins?: Plugin[]
}
``MIT
---
Note: This package uses fast-xml-parser for parsing. See fast-xml-parser for parser details.
---