Coder Social home page Coder Social logo

maxwg / fast-xml-parser-ordered Goto Github PK

View Code? Open in Web Editor NEW

This project forked from naturalintelligence/fast-xml-parser

1.0 0.0 1.0 2.26 MB

Fast-Xml-Parser, except Tags are processed in order and returned as an array rather than object

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

License: Other

JavaScript 93.34% HTML 6.66%

fast-xml-parser-ordered's Introduction

fast-xml-parser-ordered

Fast-Xml-Parser, except Tags are processed in order and returned as an array rather than object.

Example XML:

<note>
    <text>the</text>
    <text>quick</text>
    <b>brown</b>
    <b>fox</b>
    <i>jumps</b>
    <b>
        <i>over</i>
    </b>
    <text>the lazy dog</text>
</note>

Result:

{  
   "tag":"!xml",
   "children":[  
      {  
         "tag":"note",
         "children":[  
            {  
               "tag":"text",
               "#text":"the"
            },
            {  
               "tag":"text",
               "#text":"quick"
            },
            {  
               "tag":"b",
               "#text":"brown"
            },
            {  
               "tag":"b",
               "#text":"fox"
            },
            {  
               "tag":"i",
               "#text":"jumps"
            },
            {  
               "tag":"b",
               "children":[  
                  {  
                     "tag":"i",
                     "#text":"over"
                  }
               ]
            },
            {  
               "tag":"text",
               "#text":"the lazy dog"
            }
         ]
      }
   ]
}

Validate XML, Parse XML to JS/JSON and vice versa, or parse XML to Nimn rapidly without C/C++ based libraries and no callback

This project welcomes contributors. If you have a feature you'd like to see implemented or a bug you'd liked fixed, the best and fastest way to make that happen is to implement it and submit a PR. Basic knowledge of JS is sufficient. Feel free to ask for any guidance.

Users

List of some applications/projects using Fast XML Parser. (Raise an issue to submit yours)

Join this project as collaborator / maintainer.

Backers on Open Collective Sponsors on Open Collective Known Vulnerabilities NPM quality Travis ci Build Status Coverage Status Try me NPM total downloads

Become a Patron! Stubmatic donate button

Main Features

FXP logo

  • Validate XML data syntactically
  • Transform XML to JSON or Nimn
  • Transform JSON back to XML
  • Works with node packages, in browser, and in CLI (press try me button above for demo)
  • Faster than any pure JS implementation.
  • It can handle big files (tested up to 100mb).
  • 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, hexadecimal, or boolean. And can optionally decode for HTML char.
    • You can remove namespace from tag or attribute name while parsing
    • It supports boolean attributes, if configured.

How to use

To use it in NPM package install it first

$npm install fast-xml-parser or using yarn $yarn add fast-xml-parser

To use it from CLI Install it globally with -g option.

$npm install fast-xml-parser -g

To use it on a webpage include it from a CDN

XML to JSON

var jsonObj = parser.parse(xmlData [,options] );
var parser = require('fast-xml-parser');
var he = require('he');

var options = {
    attributeNamePrefix : "@_",
    attrNodeName: "attr", //default is 'false'
    textNodeName : "#text",
    ignoreAttributes : true,
    ignoreNameSpace : false,
    allowBooleanAttributes : false,
    parseNodeValue : true,
    parseAttributeValue : false,
    trimValues: true,
    cdataTagName: "__cdata", //default is 'false'
    cdataPositionChar: "\\c",
    localeRange: "", //To support non english character in tag/attribute values.
    parseTrueNumberOnly: false,
    attrValueProcessor: a => he.decode(a, {isAttributeValue: true}),//default is a=>a
    tagValueProcessor : a => he.decode(a) //default is a=>a
};

if( parser.validate(xmlData) === true) { //optional (it'll return an object in case it's not valid)
    var jsonObj = parser.parse(xmlData,options);
}

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

Note: he library is used in this example

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 : This options has been removed from 3.3.4. Instead, use tagValueProcessor, and attrValueProcessor. See above example.
  • cdataTagName : If specified, parser parse CDATA as nested tag instead of adding it's value to parent tag.
  • cdataPositionChar : It'll help to covert JSON back to XML without losing CDATA position.
  • localeRange: Parser will accept non-English character in tag or attribute name. Check #87 for more detail. Eg localeRange: "a-zA-Zа-яёА-ЯЁ"
  • parseTrueNumberOnly: if true then values like "+123", or "0123" will not be parsed as number.
  • tagValueProcessor : Process tag value during transformation. Like HTML decoding, word capitalization, etc. Applicable in case of string only.
  • attrValueProcessor : Process attribute value during transformation. Like HTML decoding, word capitalization, etc. Applicable in case of string only.
  • stopNodes : an array of tag names which are not required to be parsed. Instead their values are parsed as string.
To use from command line
$xml2js [-ns|-a|-c|-v|-V] <filename> [-o outputfile.json]
$cat xmlfile.xml | xml2js [-ns|-a|-c|-v|-V] [-o outputfile.json]
  • -ns : To include namespaces (by default ignored)
  • -a : To ignore attributes
  • -c : To ignore value conversion (i.e. "-3" will not be converted to number -3)
  • -v : validate before parsing
  • -V : only validate
To use it on webpage
var result = parser.validate(xmlData);
if (result !== true) console.log(result.err);
var jsonObj = parser.parse(xmlData);

JSON / JS Object to XML

var Parser = require("fast-xml-parser").j2xParser;
//default options need not to set
var defaultOptions = {
    attributeNamePrefix : "@_",
    attrNodeName: "@", //default is false
    textNodeName : "#text",
    ignoreAttributes : true,
    cdataTagName: "__cdata", //default is false
    cdataPositionChar: "\\c",
    format: false,
    indentBy: "  ",
    supressEmptyNode: false,
    tagValueProcessor: a=> he.encode(a, { useNamedReferences: true}),// default is a=>a
    attrValueProcessor: a=> he.encode(a, {isAttributeValue: isAttribute, useNamedReferences: true})// default is a=>a
};
var parser = new Parser(defaultOptions);
var xml = parser.parse(json_or_js_obj);
OPTIONS :

With the correct options, you can get the almost original XML without losing any information.

  • attributeNamePrefix : Identify attributes with this prefix otherwise treat them as a tag.
  • attrNodeName: Identify attributes when they are grouped under single property.
  • ignoreAttributes : Don't check for attributes. Treats everything as tag.
  • encodeHTMLchar : This option has been removed from 3.3.4. Use tagValueProcessor, and attrValueProcessor instead. See above example.
  • cdataTagName : If specified, parse matching tag as CDATA
  • cdataPositionChar : Identify the position where CDATA tag should be placed. If it is blank then CDATA will be added in the last of tag's value.
  • format : If set to true, then format the XML output.
  • indentBy : indent by this char when format is set to true
  • supressEmptyNode : If set to true, tags with no value (text or nested tags) are written as self closing tags.
  • tagValueProcessor : Process tag value during transformation. Like HTML encoding, word capitalization, etc. Applicable in case of string only.
  • attrValueProcessor : Process attribute value during transformation. Like HTML encoding, word capitalization, etc. Applicable in case of string only.

Benchmark

XML to JSON

npm_xml2json_compare

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.

JSON to XML

npm_xml2json_compare

report
file size fxp 3.2 js to xml xml2js 0.4.19 builder
1.3k 160148.9801 10384.99401
1.1m 173.6374831 8.611884025

Worth to mention

  • BigBit standard) : A standard to reprent any number in the universe in comparitively less space and without precision loss. A standard to save space to represent any text string in comparision of UTF encoding.
  • imglab : Speedup and simplify image labeling / annotation. Supports multiple formats, one click annotation, easy interface and much more. There are more than 20k images are annotated every month.
  • अनुमार्गक (anumargak) : The fastest and simple router for node js web frameworks with many unique features.
  • stubmatic : A stub server to mock behaviour of HTTP(s) / REST / SOAP services, incuding DynamoDB calls. You can also mock binary formats.
  • मुनीम (Muneem) : A webframework made for all team members. Faster tha fastify, express, koa, hapi and others.
  • शब्दावली (shabdawali) : Amazing human like typing effects beyond your imagination.

Contributors

This project exists thanks to all the people who contribute. [Contribute].

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

fast-xml-parser-ordered's People

Contributors

alexerling avatar alfonsomunozpomer avatar amitguptagwl avatar bb avatar brainsiq avatar delagen avatar driehle avatar francescobarbera avatar j0k3r avatar jinq102030 avatar juxe avatar kiro55 avatar kurtpreston avatar lexmark-haputman avatar littlelan2 avatar lplazas avatar maxwg avatar monkeywithacupcake avatar recca0120 avatar robhaj avatar serggonin avatar tatsh avatar tdawgy avatar thedeathconqueror avatar tomasbruckner avatar trentshell avatar werehamster avatar ziishaned avatar

Stargazers

 avatar

Forkers

artenuvielle

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.