xml-to-tree
v1.0.0
Published
A utility for parsing XML strings into a tree structure - safe for fragments and multi-root.
Maintainers
Readme
xml-to-tree
Canonical URL:
https://alexstevovich.com/a/xml-to-tree-nodejs
Software URL:
https://midnightcitylights.com/software/xml-to-tree-nodejs
xml-to-tree is a utility for parsing XML strings into a JSON tree representation. It is safe for XML fragments and multi-root documents, providing a clean structure for each node and its children.
Installation
npm install xml-to-treeExample
import { parse } from 'xml-to-tree';
const xml = `
<book>
<title>XML for Beginners</title>
<author>John Doe</author>
<year>2025</year>
</book>`;
const tree = parse(xml);
console.log(JSON.stringify(tree, null, 2));Function
parse(xml, options = {})
xml: The XML string or fragment to parse.options.tagKey: The key used for the tag name (default is'tag').options.childrenKey: The key used for child nodes (default is'children').options.textKey: The key used for text content (default is'text').
Returns a JSON tree representation of the XML. It processes each element node, its attributes, and its child nodes recursively.
License
Licensed under the MIT License.
