/fast-xml-parser

Validate or Parse XML to JS/JSON very fast without C/C++ based libraries

Primary LanguageJavaScriptMIT LicenseMIT

Validae XML or Parse XML to JS/JSON very fast without C/C++ based libraries and no callback

You can use this library online (press try me button above), or as command from CLI, or in your website, or in npm repo.

  • This library let you validate the XML data syntatically.
  • Or you can transform/covert/parse XML data to JS/JSON object.
  • Or you can transform the XML in traversable JS object which can later be converted to JS/JSON object.

Code Climate Stubmatic donate button Known Vulnerabilities Travis ci Build Status Coverage Status Try me bitHound Dev Dependencies bitHound Overall Score

How to use

Installation

$npm install fast-xml-parser

or using yarn

$yarn add fast-xml-parser

Usage

var fastXmlParser = require('fast-xml-parser');
var jsonObj = fastXmlParser.parse(xmlData);

// when a tag has attributes
var options = {
		attrPrefix : "@_",
        textNodeName : "#text",
		ignoreNonTextNodeAttr : true,
        ignoreTextNodeAttr : true,
		ignoreNameSpace : true
	};
if(fastXmlParser.validate(xmlData)=== true){//optional
	var jsonObj = fastXmlParser.parse(xmlData,options);
}

//Intermediate obj
var tObj = fastXmlParser.getTraversalObj(xmlData,options);
var jsonObj = fastXmlParser.convertToJson(tObj);

To use from command line

$xml2js [-ns|-a] <filename> [-o outputfile.json]

To use it on webpage

  1. Download and include parser.js
var isValid = parser.validate(xmlData);
var jsonObj = parser.parse(xmlData);

Give me a star, if you really like this project.

Comparision

I decided to created this library when I couldn't find any library which can convert XML data to json without any callback and which is not based on any C/C++ library.

Liraries that I compared

  • xml-mapping : fast, result is not satisfactory
  • xml2js : fast, result is not satisfactory
  • xml2js-expat : couldn't test performance as it gives error on high load. Instalation failed on traivs and on my local machine using 'yarn'.
  • xml2json : based on node-expat which is based on C/C++. Instalation failed on travis.
  • fast-xml-parser : very very fast.

Why not C/C++ based libraries? Instalation of such libraries fails on some OS. You may require to install missing dependency manually.

Benchmark report npm_xml2json_compare

Don't forget to check the performance report on comparejs.

validator benchmark: 2000 tps

Limitation

This tool doesn't check if the XML is valid or not. If the XML is not valid you may get invalid result.

Report an issue or request for a feature here

Your contribution in terms of donation, testing, bug fixes, code development etc. can help me to write fast algorithms. Stubmatic donate button

Some of my other NPM pojects

  • stubmatic : A stub server to mock behaviour of HTTP(s) / REST / SOAP services. Stubbing redis is on the way.
  • compare js : comparethe featues of JS code, libraries, and NPM repos.
  • fast-lorem-ipsum : Generate lorem ipsum words, sentences, paragraph very quickly.