Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fast-xml-parser

Package Overview
Dependencies
Maintainers
1
Versions
136
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-xml-parser - npm Package Compare versions

Comparing version 1.1.1 to 2.0.0

.vscode/settings.json

118

lib/parser.js
var getAllMatches = function(string, regex) {
//var regex = new RegExp(regex_str,"g");
var matches = [];
var match;
while (match = regex.exec(string)) {
var match = regex.exec(string);
while (match) {
var allmatches = [];

@@ -11,39 +11,59 @@ for (var index = 0; index < match.length; index++) {

matches.push(allmatches);
match = regex.exec(string);
}
return matches;
}
};
var Node = function(tagname,parent){
var xmlNode = function(tagname,parent,val){
this.tagname = tagname;
this.parent = parent;
this.child = [];
//this.val = "";
this.val = val;
this.addChild = function (child){
this.child.push(child);
}
}
};
};
var tagsRegx = new RegExp("<(\\/?[a-zA-Z0-9_:]+)","g");
//var tagsRegx = new RegExp("<(\\/?[^\S<>]+)","g");
var valsRegx = new RegExp(">([^<]+)<","g");
var tagsRegx = new RegExp("<(\\/?[a-zA-Z0-9_:]+)([^>\\/]*)(\\/?)>([^<]+)?","g");
var defaultOptions = {
attrPrefix : "@_",
textNodeName : "#text",
ignoreNonTextNodeAttr : true,
ignoreTextNodeAttr : true,
ignoreNameSpace : true,
ignoreRootElement : false
};
var xml2json = function (xmlData){
xmlData = xmlData.replace(/>(\s+)/g, "");//Remove spaces and make it single line.
var buildOptions = function (options){
if(!options) options = {};
var props = ["attrPrefix","ignoreNonTextNodeAttr","ignoreTextNodeAttr","ignoreNameSpace","ignoreRootElement","textNodeName"];
for (var i = 0; i < props.length; i++) {
if(options[props[i]] === undefined){
options[props[i]] = defaultOptions[props[i]];
}
}
return options;
};
var xml2json = function (xmlData,options){
options = buildOptions(options);
xmlData = xmlData.replace(/>(\s+)/g, ">");//Remove spaces and make it single line.
var tags = getAllMatches(xmlData,tagsRegx);
var values = getAllMatches(xmlData,valsRegx);
// for (var j = 0; j < values.length; j++) {
// console.log(values[j][1]);
// }
var rootNode = new Node(tags[0][1]);
var rootNode = new xmlNode(tags[0][1]);
var currentNode = rootNode;
var selfClosingTag = false;
for (var i = 1,j=0; i < tags.length -1 ; i++) {
var tag = tags[i][1];
var nexttag = tags[i+1][1];
if( ("/" + tag) === nexttag){ //leaf node
var val;
if(values[j]){
val = values[j++][1];
var selfClosingTag = tags[i][3] === '/';
if(selfClosingTag){
var childNode = new xmlNode(tag,currentNode);
var attrs = buildAttributesArr(tags[i][2],options.ignoreTextNodeAttr,options.attrPrefix);
childNode.val = attrs || "";
currentNode.addChild(childNode);
}else if( ("/" + tag) === nexttag || selfClosingTag){ //Text node
var val =tags[i][4];
if(val){
if(isNaN(val)){

@@ -58,12 +78,29 @@ val = "" + val ;

}
}else{
val = "";
}
var childNode = new Node(tag,currentNode);
childNode.val = val;
var childNode = new xmlNode(tag,currentNode);
var attrs = buildAttributesArr(tags[i][2],options.ignoreTextNodeAttr,options.attrPrefix);
if(attrs){
attrs[options.textNodeName] = val;
childNode.val = attrs;
}else{
childNode.val = val || "";
}
currentNode.addChild(childNode);
i++;
}else if(tag.indexOf("/") === 0){
}else if(tag.indexOf("/") === 0){//ending tag
currentNode = currentNode.parent;
continue;
}else{
var cNode = new Node(tag,currentNode);
}else{//starting tag
var cNode = new xmlNode(tag,currentNode);
var attrs = buildAttributesArr(tags[i][2],options.ignoreNonTextNodeAttr,options.attrPrefix);
if(attrs){
for (var property in attrs) {
if (attrs.hasOwnProperty(property)) {
cNode.addChild(new xmlNode(property,cNode,attrs[property]));
}
}
}
currentNode.addChild(cNode);

@@ -74,6 +111,19 @@ currentNode = cNode;

//include root node as well
var xmlObj = new Node('_xml');
var xmlObj = new xmlNode('_xml');
rootNode.param = xmlObj;
xmlObj.addChild(rootNode);
return convertToJson(xmlObj);
};
var attrsRegx = new RegExp("(\\S+)=.([^'\"]+)","g");
function buildAttributesArr(attrStr,ignore,prefix){
attrStr = attrStr || attrStr.trim();
if(!ignore && attrStr.length > 3){
var matches = getAllMatches(attrStr,attrsRegx);
var attrs = {};
for (var i = 0; i < matches.length; i++) {
attrs[prefix + matches[i][1]] = matches[i][2];
}
return attrs;
}
}

@@ -83,3 +133,3 @@

var jObj = {};
if(node.val) {
if(node.val || node.val === "") {
return node.val;

@@ -105,6 +155,6 @@ }else{

if(typeof exports === "undefined"){
exports = this;
}
exports.parse = xml2json;
// (function debug(){
// var xmlData = "<rootNode><tag>value</tag><intTag>45</intTag><floatTag>65.34</floatTag></rootNode>";
// var result = exports.parse(xmlData);
// })();
{
"name": "fast-xml-parser",
"version": "1.1.1",
"version": "2.0.0",
"description": "Parse XML to JS/JSON very fast without C/C++ based libraries",

@@ -28,6 +28,8 @@ "main": "./lib/parser.js",

"coveralls": "^2.11.15",
"http-server": "^0.9.0",
"istanbul": "^0.4.5",
"jasmine": "^2.5.3",
"jasmine-core": "^2.5.2"
"jasmine-core": "^2.5.2",
"zombie": "^5.0.5"
}
}

@@ -5,2 +5,4 @@ # [fast-xml-parser](https://www.npmjs.com/package/fast-xml-parser)

[![Code Climate](https://codeclimate.com/github/NaturalIntelligence/fast-xml-parser/badges/gpa.svg)](https://codeclimate.com/github/NaturalIntelligence/fast-xml-parser) [<img src="https://www.paypalobjects.com/webstatic/en_US/btn/btn_donate_92x26.png" alt="Stubmatic donate button"/>](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=KQJAX48SPUKNC) [![Known Vulnerabilities](https://snyk.io/test/github/naturalintelligence/fast-xml-parser/badge.svg)](https://snyk.io/test/github/naturalintelligence/fast-xml-parser) [![Travis ci Build Status](https://travis-ci.org/NaturalIntelligence/fast-xml-parser.svg?branch=master)](https://travis-ci.org/NaturalIntelligence/fast-xml-parser) [![Coverage Status](https://coveralls.io/repos/github/NaturalIntelligence/fast-xml-parser/badge.svg?branch=master)](https://coveralls.io/github/NaturalIntelligence/fast-xml-parser?branch=master) [<img src="https://img.shields.io/badge/Try-me-blue.svg?colorA=FFA500&colorB=0000FF" alt="Try me"/>](https://naturalintelligence.github.io/fast-xml-parser/)
[![bitHound Dev Dependencies](https://www.bithound.io/github/NaturalIntelligence/fast-xml-parser/badges/devDependencies.svg)](https://www.bithound.io/github/NaturalIntelligence/fast-xml-parser/master/dependencies/npm)
[![bitHound Overall Score](https://www.bithound.io/github/NaturalIntelligence/fast-xml-parser/badges/score.svg)](https://www.bithound.io/github/NaturalIntelligence/fast-xml-parser)

@@ -17,2 +19,12 @@ ### How to use

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

@@ -19,0 +31,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc