I know there are probably 13.5 Million of these already, but I wanted to do one myself, okay?
If you have the XMLtoJSON.js file locally, you can import and use it as an ES module:
// Import the function (ES module)
import { XMLtoJSON } from './XMLtoJSON.js';
const xmlString = `<note><to>User</to><from>AI</from><body>Hello!</body></note>`;
const result = XMLtoJSON(xmlString);
console.log(result);First, install the package:
npm install @mattlag/xmltojsonThen import and use it in your project:
// Import from node_modules (ES module)
import { XMLtoJSON } from '@mattlag/xmltojson';
const xmlString = `<note><to>User</to><from>AI</from><body>Hello!</body></note>`;
const result = XMLtoJSON(xmlString);
console.log(result);Note: The package is published under the
@mattlagscope. Use@mattlag/xmltojsonin your import and install commands.
The XMLtoJSON function accepts a second argument, trimOptions, which lets you control how whitespace, newlines, and tabs are handled in the parsed output. This is useful for cleaning up text content from XML nodes.
Available options:
trimWhitespace(default: true): Remove leading and trailing spaces from text.trimNewlines(default: true): Remove all newline characters (\n, \r).trimTabs(default: true): Remove all tab characters (\t).
If you omit trimOptions, all three are enabled by default.
{
name : 'xml_tag_name',
attributes : {
'xml_attribute_name1' : 'XML Attribute value1',
'xml_attribute_name2' : 'XML Attribute value2',
'xml_attribute_name3' : 'XML Attribute value3',
...
},
content : [
{ xml_tags },
{ xml_tags },
...,
'text content'
]
}A proper JavaScript SyntaxError will be thrown if the XML is not properly formatted.
XMLtoJSON is really focused on data, and not so much the metadata. So I did this:
- All comments are ignored <!-- stuff like this -->
- All declarations are ignored, stuff like <?xml version="1.0" encoding="UTF-8"?>
- All CDATA sections are ignored, stuff like <![CDATA[<greeting>Hello, world!</greeting>]]>
- Newline and Tab characters, like \r \n \t, are blindly removed from everywhere
Copyright © 2026 Matthew LaGrandeur
Released under GPL 3.0
| Matthew LaGrandeur |
| matt[at]mattlag[dot]com |