Fast-XML-Parser serves as a swift, JavaScript-based utility enabling developers to validate, parse, and build XML data. It carries out these operations without requiring any C/C++ based libraries. Boasting a faster performance than any other pure JavaScript implementation, it can handle large files (which have been tested up to 100mb in size), supporting controlled parsing using various options. It supports XML Entities, HTML entities, and DOCTYPE entities and can accommodate unpaired tags, stop nodes, among other elements. Importantly, Fast-XML-Parser can almost restore the original XML from a JSON object while preserving the order of tags in the JS object.
Using Fast-XML-Parser involves installing it and integrating it into your codebase. For package dependency, you can use the command $ npm install fast-xml-parser
or $ yarn add fast-xml-parser
. For system command utility, employ $ npm install fast-xml-parser -g
.
Example code for a Node.js project:
const { XMLParser, XMLBuilder, XMLValidator} = require("fast-xml-parser");
const parser = new XMLParser();
let jObj = parser.parse(XMLdata);
const builder = new XMLBuilder();
const xmlContent = builder.build(jObj);
For HTML page usage, you would include the script from a CDN and utilize the parser as follows:
<script src="path/to/fxp.min.js"></script>
:
<script>
const parser = new fxparser.XMLParser();
parser.parse(xmlContent);
</script>
As a CLI command, you can use fxparser some.xml
.
The documentation for Fast-XML-Parser resides directly in the GitHub repository. You can find detailed instructions for using the library in the docs
folder, which contains separate documentations for versions 3 and 4 of the library; v3 documents and v4 documents.