Fast XML Parser

Amit Kumar Gupta
4 min readFeb 14, 2018

--

Parse \ Validate XML into JS \ JSON rapidly.

> Fast XML Parser v4.2 feature with examples

Fast XML Parser (FXP)is the JS library which validate and parse a XML syntactically without applying the rules of XSD. It has completed it’s one year with 85k Downloads and 175+ github stars. There are 200+ known projects and 2000+ unknown projects (based on the dependency & download pattern) which are using this library.

Update on 20 Oct 2019

Fast XML Parser is being used by many big organizations and projects like. It has more than 500 stars on Github and more than 60k weekly downloads.

Main Features

  • Validate XML data syntactically
  • Transform XML to JSON or Nimn
  • Transform JSON back to XML
  • Works with node packages, in browser, and in CLI
  • Faster than any pure JS implementation.
  • It can handle big files (tested up to 100mb). Some people has tested it for 800 mb files

Various options are available to customize the transformation

  • You can parse CDATA as separate property.
  • You can prefix attributes or group them to separate property. Or can ignore them from result completely.
  • You can parse tag’s or attribute’s value to primitive type: string, integer, float, or boolean.
  • You can set your own value processor for HTML decoding, camel casing, capitalization etc.
  • You can remove namespace from tag or attribute name while parsing
  • It supports boolean attributes.

History

One of major features of Stubmatic was to make it QA friendly and avoid coding as much as possible. Last year, when a user raised an issue about failing installation due to some C/C++ based dependencies, I started looking for a pure JS based parser for JSON and XML.

As the Stubmatic was being used in performance testing as well, and it was challenging to get higher performance from pure JS based solution, I decided to write Fast XML Parser and Fast Lorem Ipsum generator.

Performance

Usage

To use it in NPM package

var parser = require('fast-xml-parser');//default options, need not to set
var options = {
attributeNamePrefix : "@_",
attrNodeName: false,
textNodeName : "#text",
ignoreAttributes : true,
ignoreNameSpace : false,
allowBooleanAttributes : false,
parseNodeValue : true,
parseAttributeValue : false,
trimValues: true,
cdataTagName: "__cdata", //default is 'false'
cdataPositionChar: "\\c",
attrValueProcessor: a => a.toUpperCase(),
tagValueProcessor : a => a.toUpperCase()
}
if(parser.validate(xmlData)=== true){//optional
var jsonObj = parser.parse(xmlData,options);
}

To use it from command line

$xml2js [-ns|-a|-c] <filename> [-o outputfile.json]
$cat xmlfile.xml | xml2js [-ns|-a|-c] [-o outputfile.json]

To use it on webpage

Download and include parser.js Or use it directly from CDN

var result = parser.validate(xmlData);
if(result !== true) cnosole.log(result.err);
var jsonObj = parser.parse(xmlData);

So is it the best library?

No it is not. Fast XML parser is good when

  • You want a single library at both server and client side.
  • You want a simplified dependency tree.
  • You don’t want to bother the user with installation of multiple OS related dependencies.
  • You want to run the code in a browser.
  • You need many parsing features like customization, encoding, or locale etc.

So what are the other options?

I found camaro is a good and faster alternative, if you are looking for only server side solution. It’s stat says it’s faster than FXP. However I couldn’t test it due to the installation failure on my local machine.

Try this. It is free, opensource, and under MIT license.

I’ll love your appreciation by visiting the Github repository, telling to your friends, or clap this post (Tip: you can clap many times as long as you want). Don’t forget to bookmark the repository for further updates.

Please note that

As some of the users are confused, we’re not converting XML into JSON here but in JS object and vice versa. In simple words, JSON can be considered as string representation of JS object as an object need to be serialized before storing or transporting. However, you can use CLI version to convert XML to JSON.

JSON is having many advantages over XML. One of them is that the size of JSON is smaller than XML. Small size saves disk storage, network bandwidth, and speed up communication. If you’re really concerned about data size then you should also read about निम्न (Nimn) Data format. It is approx 80% less in comparison of XML.

Feedback

Feedbacks are important to understand how can I improve and bring more useful materials. Please clap this article, comment here, or please give a star to FXP github repository.

--

--

Amit Kumar Gupta
Amit Kumar Gupta

Written by Amit Kumar Gupta

Umm.. seeking for some research work. You can find some on SoloThought.com

No responses yet