Coder Social home page Coder Social logo

jkso / fast-xml-parser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from naturalintelligence/fast-xml-parser

0.0 2.0 0.0 979 KB

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

Home Page: https://naturalintelligence.github.io/fast-xml-parser/

License: MIT License

JavaScript 96.51% HTML 3.49%

fast-xml-parser's Introduction

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

**Note**: If you are using v3, your code may start failing in parsing and validation both. I apologize for the breaking changes. But code was supposed to be changed to support large files and many other options. Please refer the code example below for more detail.

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 syntactically.
  • 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 Donate using Liberapay Known Vulnerabilities NPM quality Travis ci Build Status Coverage Status Try me bitHound Dev Dependencies bitHound Overall Score NPM total downloads

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);

/* upto 2.9.x
var options = {
    attrPrefix : "@_",
    attrNodeName: false,
    textNodeName : "#text",
    ignoreNonTextNodeAttr : true,
    ignoreTextNodeAttr : true,
    ignoreNameSpace : true,
    ignoreRootElement : false,
    textNodeConversion : true,
    textAttrConversion : false,
    arrayMode : false
};
*/
//from 3.0.0
var options = {
    attributeNamePrefix : "@_",
    attrNodeName: false,
    textNodeName : "#text",
    ignoreAttributes : true,
    ignoreNameSpace : false,
    allowBooleanAttributes : false,
    parseNodeValue : true,
    parseAttributeValue : false,
    trimValues: true,
    decodeHTMLchar: false,
};
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,options);

OPTIONS :

  • attributeNamePrefix : prepend given string to attribute name for identification
  • attrNodeName: (Valid name) Group all the attributes as properties of given name.
  • ignoreAttributes : Ignore attributes to be parsed.
  • ignoreNameSpace : Remove namespace string from tag and attribute names.
  • allowBooleanAttributes : a tag can have attributes without any value
  • parseNodeValue : Parse the value of text node to float, integer, or boolean.
  • parseAttributeValue : Parse the value of an attribute to float, integer, or boolean.
  • trimValues : trim string values of an attribute or node
  • decodeHTMLchar : decodes any named and numerical character HTML references excluding CDATA part.

To use from command line

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

-ns : To include namespaces (bedefault ignored) -a : To ignore attributes -c : To ignore value conversion (i.e. "-3" will not be converted to number -3)

To use it on webpage

  1. Download and include parser.js
var result = parser.validate(xmlData);
if(result !== true) cnosole.log(result.err);
var jsonObj = parser.parse(xmlData);

Or use directly from CDN

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.

Libraries 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. Installation failed on travis and on my local machine using 'yarn'.
  • xml2json : based on node-expat which is based on C/C++. Installation failed on travis.
  • fast-xml-parser : very very fast.

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

Benchmark report

file size fxp 3.0 validator (rps) fxp 3.0 parser (rps) xml2js 0.4.19 (rps)
1.5k 16581.06758 14032.09323 4615.930805
1.5m 14918.47793 13.23366098 5.90682005
13m 1.834479235 1.135582008 -1
1.3k with CDATA 30583.35319 43160.52342 8398.556349
1.3m with CDATA 27.29266471 52.68877009 7.966000795
1.6k with cdata,prolog,doctype 27690.26082 41433.98547 7872.399268
98m 0.08473858148 0.2600104004 -1
  • -1 indicates error or incorrect output.

npm_xml2json_compare

npm_xml2json_compare

Changes from v3

  • It can handle big file now (I have tested up to 98mb). Performance report is given above.
  • Meaningful error messages from validator
"err": {
    "code": "InvalidAttr",
    "msg": "Attributes for rootNode have open quote"
}
  • Updated options : check snippet aboove
  • Parse boolean values as well. E.g. "true" to true
  • You can set pasrer not to trim whitespaces from attribute or tag /node value.
  • You can set pasrer to HTML decode Tag / node and attribute values. However CDATA value will not be HTML decoded.
  • Tag / node value will not be parsed if CDATA presents.
  • You can set validator and parser to allow boolean values.
  • Few validation and parsing bugs are also fixed

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.
  • fast-lorem-ipsum : Generate lorem ipsum words, sentences, paragraph very quickly.

TODO

  • P2: validating XML stream data
  • P2: validator cli
  • P2: fast XML prettyfier

fast-xml-parser's People

Contributors

amitguptagwl avatar delagen avatar tatsh avatar alfonsomunozpomer avatar j0k3r avatar kiro55 avatar

Watchers

James Cloos avatar pylonkin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.