$ cnpm install simple-xml-to-json
Simply install using NPM in your project directory
npm install simple-xml-to-json
xmlToConvert <string>customConverter <function>customConverter is usedxmlToConvert <string>CommonJS (Node.js):
const { convertXML, createAST } = require("simple-xml-to-json")
const myJson = convertXML(myXMLString)
const myYaml = convertXML(myXMLString, yamlConverter)
const myAst = createAST(myXMLString)
ESM (modern frontend apps, Node.js with "type": "module", bundlers):
import { convertXML, createAST } from "simple-xml-to-json"
const myJson = convertXML(myXMLString)
const myYaml = convertXML(myXMLString, yamlConverter)
const myAst = createAST(myXMLString)
node example/example.js (CJS) or node example/example.mjs (ESM) in your terminal and see what happens.xmlToJson.js file for convenience. Just pass in the XML as a String.Take these results with a grain of salt.
According to a simple benchmark test I performed in April 2024 with a random XML. YMMV.

By default, an element's text content is mapped to a "content" property, while its attributes are mapped directly to JSON properties of the same name.
If an XML element possesses an attribute actually named content while also containing text content, a name collision occurs. This typically results in a parsing error or data loss due to duplicate keys.
Example Collision:
<MyElement content="attr-value">text-content</MyElement>
To prevent these collisions, versions newer than 1.2.3 will specifically prefix the "content" attribute name with an @ symbol. So the attribute "content" now becomes the "@content" JSON property.
[!NOTE] If you need to, you can write your own converter from the AST created by the parser, and pass it as a 2nd parameter after the xml string
Copyright 2013 - present © cnpmjs.org | Home |