fast-xml-parser
Advanced tools
Comparing version 2.2.1 to 2.3.0
@@ -12,3 +12,3 @@ { | ||
"name": "Launch Program", | ||
"program": "${workspaceRoot}/lib/parser.js", | ||
"program": "${workspaceRoot}/lib/validator.js", | ||
"cwd": "${workspaceRoot}" | ||
@@ -15,0 +15,0 @@ }, |
@@ -26,4 +26,4 @@ #!/usr/bin/env node | ||
}else if(process.argv[i] === "-a"){ | ||
options.ignoreNonTextNodeAttr = false; | ||
options.ignoreTextNodeAttr = false; | ||
options.ignoreNonTextNodeAttr = true; | ||
options.ignoreTextNodeAttr = true; | ||
}else if(process.argv[i] === "-o"){ | ||
@@ -30,0 +30,0 @@ outputFileName = process.argv[++i]; |
@@ -1,16 +0,14 @@ | ||
var getAllMatches = function(string, regex) { | ||
//var regex = new RegExp(regex_str,"g"); | ||
var matches = []; | ||
var match = regex.exec(string); | ||
while (match) { | ||
var allmatches = []; | ||
for (var index = 0; index < match.length; index++) { | ||
allmatches.push(match[index]); | ||
} | ||
matches.push(allmatches); | ||
match = regex.exec(string); | ||
} | ||
return matches; | ||
}; | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.parser = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var InvalidXmlException = function (msg){ | ||
this.name = "InvalidXmlException"; | ||
this.message = msg; | ||
this.stack = (new Error()).stack; | ||
} | ||
InvalidXmlException.prototype = Object.create(Error.prototype); | ||
InvalidXmlException.prototype.constructor = InvalidXmlException; | ||
module.exports = InvalidXmlException; | ||
},{}],2:[function(require,module,exports){ | ||
var getAllMatches = require("./util").getAllMatches; | ||
var xmlNode = function(tagname,parent,val){ | ||
@@ -96,3 +94,3 @@ this.tagname = tagname; | ||
return xmlObj; | ||
} | ||
}; | ||
@@ -168,10 +166,158 @@ var xml2json = function (xmlData,options){ | ||
return jObj; | ||
}; | ||
exports.parse = xml2json; | ||
exports.getTraversalObj = getTraversalObj; | ||
exports.convertToJson = convertToJson; | ||
exports.validate = require("./validator").validate; | ||
},{"./util":3,"./validator":4}],3:[function(require,module,exports){ | ||
var getAllMatches = function(string, regex) { | ||
//var regex = new RegExp(regex_str,"g"); | ||
var matches = []; | ||
var match = regex.exec(string); | ||
while (match) { | ||
var allmatches = []; | ||
for (var index = 0; index < match.length; index++) { | ||
allmatches.push(match[index]); | ||
} | ||
matches.push(allmatches); | ||
match = regex.exec(string); | ||
} | ||
return matches; | ||
}; | ||
exports.getAllMatches = getAllMatches; | ||
},{}],4:[function(require,module,exports){ | ||
var getAllMatches = require("./util").getAllMatches; | ||
var InvalidXmlException = require("./InvalidXmlException"); | ||
var validate = function (xmlData){ | ||
xmlData = xmlData.replace(/[ \t]/g, " "); | ||
var eStack = [], currentTag = "", lineNum = 1; | ||
for (var i = 0; i < xmlData.length;i++) { | ||
if(xmlData[i] === "\n"){ | ||
lineNum++; | ||
}else if(xmlData[i] === '<'){ | ||
if(xmlData[i+1] === " "){//comment tag | ||
throw new InvalidXmlException("Invalid tag at "+ lineNum + ":" + i); | ||
}else if(xmlData[i+1] === "!"){//comment tag | ||
var comment = getCommentTag(xmlData,i,lineNum); | ||
i+=comment.length-1; | ||
}else if(xmlData[i+1] === "/"){//closing tag | ||
i+=2; | ||
currentTag = getEndTagName(xmlData,i,lineNum); | ||
if(eStack[eStack.length-1] !== currentTag){ | ||
throw new InvalidXmlException("closing tag is not matching at "+ lineNum + ":" + i); | ||
}else{ | ||
eStack.pop(); | ||
} | ||
i+=currentTag.length; | ||
}else{ | ||
currentTag = getTagName(xmlData,++i); | ||
i+=currentTag.length; | ||
var attrStr = getAttrStr(xmlData,i,lineNum); | ||
if(attrStr && (attrStr[attrStr.length-1] === "/"|| attrStr[attrStr.length-1] === "?")){ | ||
i+=attrStr.length; | ||
}else{ | ||
eStack.push(currentTag); | ||
} | ||
var text = getvalue(xmlData,++i); | ||
i+=text.length-1; | ||
} | ||
} | ||
} | ||
if(eStack.length === 0) return true; | ||
else | ||
throw new InvalidXmlException("closing tag is missing for "+ eStack); | ||
}; | ||
/** | ||
* Validate and return comment tag | ||
*/ | ||
function getCommentTag(xmlData,startIndex,lineNum){ | ||
for (var i = startIndex; i < xmlData.length; i++){ | ||
if(xmlData[i] === "-" && xmlData[i+1] === "-" && xmlData[i+2] === ">") break; | ||
} | ||
if(xmlData.substr(startIndex,4) === "<!--" && xmlData.substr(i,3) === "-->") | ||
return xmlData.substring(startIndex,i); | ||
else | ||
throw new InvalidXmlException("Invalid comment tag at " + lineNum +":"+ startIndex); | ||
} | ||
if(typeof exports === "undefined"){ | ||
exports = this; | ||
/** | ||
* Validate and return end ending tag | ||
*/ | ||
function getEndTagName(xmlData,startIndex,lineNum){ | ||
xmlData = xmlData.replace(/\s/g, " ");for (var i = startIndex; i < xmlData.length && xmlData[i] !== " " && xmlData[i] !== ">"; i++); | ||
if(xmlData[i-1] !== ">"){ | ||
for(var j=i;j<xmlData.length && xmlData[j] !== ">"; j++){ | ||
if(xmlData[j] !== " ") | ||
throw new InvalidXmlException("Invalid closing tag at " + lineNum +":"+ startIndex); | ||
} | ||
} | ||
return xmlData.substring(startIndex,i); | ||
} | ||
exports.parse = xml2json; | ||
exports.getTraversalObj = getTraversalObj; | ||
exports.convertToJson = convertToJson; | ||
var attrsRegx1 = new RegExp('(?:[\\s]+([\\w:\-]+)[\\s]*=[\\s]*"([^"]*)")',"g"); | ||
var attrsRegx2 = new RegExp("(?:[\\s]+([\\w:\-]+)[\\s]*=[\\s]*'([^']*)')","g"); | ||
var attrNamesRegx = new RegExp("([\\w: \-]+)[\\s]*=","g"); | ||
/** | ||
* Repeated attributes are not allowed | ||
* if attribute value is enclosed in \' there can't be \' in value | ||
* if attribute value is enclosed in \" there can't be \" in value | ||
* there should be space between 2 attributs | ||
* attribute name can't have space, \', \", = | ||
*/ | ||
function getAttrStr(xmlData,startIndex,lineNum){ | ||
for (var i = startIndex; i < xmlData.length && xmlData[i] !== ">"; i++); | ||
if(xmlData[i] === ">"){ | ||
var attrStr = xmlData.substring(startIndex,i); | ||
//attrStr = attrStr.trim(); | ||
if(attrStr.length > 4){ //a="" | ||
var attrs = getListOfAttrsName([],attrStr,attrsRegx1,startIndex,lineNum); | ||
attrs = getListOfAttrsName(attrs,attrStr,attrsRegx2,startIndex,lineNum); | ||
var matches = getAllMatches(attrStr,attrNamesRegx); | ||
for (i = 0; i < matches.length; i++) { | ||
var attrName = matches[i][1].trim(); | ||
if(!attrs[attrName]) | ||
throw new InvalidXmlException("Invalid arguments at " + lineNum +":"+ startIndex); | ||
} | ||
} | ||
return attrStr; | ||
}else{ | ||
throw new InvalidXmlException("Not closing tag at " + lineNum +":"+ startIndex); | ||
} | ||
} | ||
function getListOfAttrsName(attrs,attrStr,attrsRegx,startIndex,lineNum){ | ||
var matches = getAllMatches(attrStr,attrsRegx); | ||
for (var i = 0; i < matches.length; i++) { | ||
var attrName = matches[i][1]; | ||
if(!attrs[attrName]) | ||
attrs[attrName] = true; | ||
else | ||
throw new InvalidXmlException("Argument "+ attrName +" is redefined at " + lineNum +":"+ startIndex); | ||
} | ||
return attrs; | ||
} | ||
function getTagName(xmlData,startIndex){ | ||
for (var i = startIndex; i < xmlData.length; i++){ | ||
if(xmlData[i] === " " || xmlData[i] === ">" || (xmlData[i] === "/" && xmlData[i+1] === ">")) break; | ||
} | ||
return xmlData.substring(startIndex,i); | ||
} | ||
function getvalue(xmlData,startIndex){ | ||
for (var i = startIndex; i < xmlData.length && xmlData[i] !== "<"; i++); | ||
return xmlData.substring(startIndex,i); | ||
} | ||
exports.validate = validate; | ||
},{"./InvalidXmlException":1,"./util":3}]},{},[2])(2) | ||
}); |
{ | ||
"name": "fast-xml-parser", | ||
"version": "2.2.1", | ||
"description": "Parse XML to JS/JSON very fast without C/C++ based libraries", | ||
"main": "./lib/parser.js", | ||
"version": "2.3.0", | ||
"description": "Validate XML or Parse XML to JS/JSON very fast without C/C++ based libraries", | ||
"main": "./bin/parser.js", | ||
"scripts": { | ||
"test": "jasmine spec/*spec.js", | ||
"bundle": "browserify bin/parser.js --s parser > lib/parser.js", | ||
"coverage": "istanbul cover jasmine --captureExceptions spec/*spec.js", | ||
@@ -24,7 +25,12 @@ "coverage:check": "node ./node_modules/istanbul/lib/cli.js check-coverage --branch 90 --statement 90" | ||
"xml2js", | ||
"x2js", | ||
"xml2json", | ||
"js", | ||
"traversal", | ||
"traversable", | ||
"cli", | ||
"command" | ||
"command", | ||
"validator", | ||
"validate", | ||
"transformer", | ||
"checker" | ||
], | ||
@@ -34,2 +40,3 @@ "author": "Amit Gupta (https://github.com/amitguptagwl)", | ||
"devDependencies": { | ||
"browserify": "^14.1.0", | ||
"coveralls": "^2.11.15", | ||
@@ -36,0 +43,0 @@ "http-server": "^0.9.0", |
# [fast-xml-parser](https://www.npmjs.com/package/fast-xml-parser) | ||
Parse XML to JS/JSON very fast without C/C++ based libraries and no callback | ||
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](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/) | ||
@@ -10,4 +16,7 @@ [![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) | ||
**Installation** | ||
`$npm install fast-xml-parser` | ||
or using [yarn](https://yarnpkg.com/) | ||
`$yarn add fast-xml-parser` | ||
@@ -28,3 +37,5 @@ | ||
}; | ||
var jsonObj = fastXmlParser.parse(xmlData,options); | ||
if(fastXmlParser.validate(xmlData)=== true){//optional | ||
var jsonObj = fastXmlParser.parse(xmlData,options); | ||
} | ||
@@ -46,3 +57,4 @@ //Intermediate obj | ||
```js | ||
var jsonObj = xml2json(xmlData); | ||
var isValid = parser.validate(xmlData); | ||
var jsonObj = parser.parse(xmlData); | ||
``` | ||
@@ -52,2 +64,3 @@ | ||
## 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. | ||
@@ -70,2 +83,4 @@ | ||
validator benchmark: 2000 tps | ||
### Limitation | ||
@@ -72,0 +87,0 @@ This tool doesn't check if the XML is valid or not. If the XML is not valid you may get invalid result. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92005
15
715
93
7
4