Validate XML, Parse XML to JS/JSON and vice versa, or parse XML to Nimn rapidly without C/C++ based libraries and no callback
To cover expenses, we're planning to launch FXP Enterprise edition in parallel. Watch it for further updates, if you're interested.
Check ThankYouBackers for our contributors
List of some applications/projects using Fast XML Parser. (Raise an issue to submit yours)
The list of users is collected either from the list published by Github, cummunicated directly through mails/chat , or from other resources. If you feel that your name in the above list is incorrectly published or you're not the user of this library anymore then you can inform us to remove it. We'll do the necessary changes ASAP.
To use it as an NPM package:
npm install fast-xml-parser
Or using yarn:
yarn add fast-xml-parser
To use it from a CLI install it globally with the -g
option.
npm install fast-xml-parser -g
To use it on a webpage include it from a CDN
const jsonObj = parser.parse(xmlData [,options] );
const parser = require('fast-xml-parser');
const he = require('he');
const 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",
parseTrueNumberOnly: false,
numParseOptions:{
hex: true,
leadingZeros: true,
//skipLike: /\+[0-9]{10}/
},
arrayMode: false, //"strict"
attrValueProcessor: (val, attrName) => he.decode(val, {isAttributeValue: true}),//default is a=>a
tagValueProcessor : (val, tagName) => he.decode(val), //default is a=>a
stopNodes: ["parse-me-as-string"],
alwaysCreateTextNode: false
};
if( parser.validate(xmlData) === true) { //optional (it'll return an object in case it's not valid)
let jsonObj = parser.parse(xmlData,options);
}
// Intermediate obj
const tObj = parser.getTraversalObj(xmlData,options);
let jsonObj = parser.convertToJson(tObj,options);
As you can notice in the above code, validator is not embedded with in the parser and expected to be called separately. However, you can pass true
or validation options as 3rd parameter to the parser to trigger validator internally. It is same as above example.
try{
let jsonObj = parser.parse(xmlData,options, true);
}catch(error){
console.log(error.message)
}
Validator returns the following object in case of error;
{
err: {
code: code,
msg: message,
line: lineNumber,
},
};
false
, a tag with single occurrence is parsed as an object but as an array in case of multiple occurences. When true
, a tag will be parsed as an array always excluding leaf nodes. When strict
, all the tags will be parsed as array only. When instance of RegEx
, only tags will be parsed as array that match the regex. When function
a tag name is passed to the callback that can be checked.true
, forces the parser always return a property for the textNodeName
even if there are no attributes or node children.$xml2js [-ns|-a|-c|-v|-V] <filename> [-o outputfile.json]
$cat xmlfile.xml | xml2js [-ns|-a|-c|-v|-V] [-o outputfile.json]
const result = parser.validate(xmlData);
if (result !== true) console.log(result.err);
const jsonObj = parser.parse(xmlData);
const Parser = require("fast-xml-parser").j2xParser;
//default options need not to set
const 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
rootNodeName: "element"
};
const parser = new Parser(defaultOptions);
const xml = parser.parse(json_or_js_obj);
With the correct options, you can get the almost original XML without losing any information.
when
format is set to true
true
, tags with no value (text or nested tags) are written as self closing tags.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 |
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 |
This project exists thanks to all the people who contribute. [Contribute].
Thank you to all our backers!
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
0x01 背景 笔者最近在使用 Nest.js 开发服务端程序,遇到了一些需要处理 XML 的场景,搜遍了网络上发现没有比较优雅的方式,于是摸索后将过程整理出来。 0x02 研究 Google 搜索 nest handle xml 的第一个结果是一篇中文文章: Nest 中处理 XML 类型的请求与响应 但是照猫画虎了一番,发现 TS 总是报错,可能是 body-parser-xml 实现得不算很
官方链接:https://www.npmjs.com/package/fast-xml-parser GitHub:https://github.com/NaturalIntelligence/fast-xml-parser OPTIONS attributeNamePrefix:提供预定的字符串作为属性名称前缀,例如@_,假设此时解析的属性为name,那么在解析后的json中该属性将会解析为@_
public static void main(String[] args) { String test="<BGD><JBX><JBX.1>0000930568</JBX.1><JBX.36/></JBX><MZX><MZX.1>1804000031</MZX.1><MZX.13>陈胖胖</MZX.13><MZX.14>女</MZX.14><MZX.15>26岁</MZX.15
XML解析 零、创建一个XML文件 如果学过HTML,那么这就看着很轻松了,就是由一个一个标签组成的,开头的第一行是版本和编码的声明,一般不会改变。这里我们创建一个简单的books.xml文件。 <?xml version="1.0" encoding="UTF-8"?> <books> <book id="1"> <name>老人与海</name> <au
解决 改用xml2js解析xml。 import * as xml2js from 'xml2js'; export const xmlToJson = (xml: string) => { let xmlJson = ''; xml2js.parseString(xml, { explicitArray: false }, function (err, json) { xmlJs
(1)cnpm install fast-xml-parser --save (2)在要用到的js文件中引入:const fxp = require(‘fast-xml-parser’); (3)转成 json: const result = fxp.parse(xml_string); (4) 转成String: JSON.stringify(result),
项目说明: MintLeaf-Fast是一个基于SpringBoot2.0开发的,轻量级的,前后端分离的Java快速开发平台 开箱即用,节省开发时间,提升开发效率,能够快速开发项目并交付的接私活利器 支持MySQL、Oracle、SQL Server等主流数据库 项目特点: 代码简洁,注释丰富,上手容易,提供基础模块(用户管理,角色管理,菜单管理,代码生成等8个模块),可以直接作为一个后台管理系统
Fast 是 Node.js 的一个很小的基于 TCP 消息框架的 JSON 远程调用包,可用来编写简单的基于 JSON 的 RPC 系统。 示例代码: var fast = require('fast');var server = fast.createServer();server.rpc('echo', function (fname, lname, res) { res.write(
框架说明 基于springboot+shiro+freemarker的快速开发框架,代码结构清晰,快速上手使用! 配置代码生成器,减少70%开发时间,专注业务逻辑。 前端声明式组件封装、附带文档编写 ctrl+c ctrl+v 即可使用。封装数据源,可通过url、枚举、字典直接渲染组件。代码量极少且易维护。 layui常用代码的二次封装,省略layui部分繁琐的代码! 项目演示 - 演示地址:ht
fast fast 是一个基于 spring mvc 的 API 框架。 usage <dependency> <groupId>org.smartx</groupId> <artifactId>fast-core</artifactId> <version>1.0</version></dependency> config fast config fast.api.key
Java ORM框架 大幅度提高开发效率 减少编码量 1. 极·简化DAO操作,大幅度提高编码效率;2. 支持自定义SQL,自动映射;3. 支持Redis缓存和内存缓存,自动更新缓存;5. 支持MyBatis Boolean success = UserFastDao.create().dao().insert(user); //增,新增成功后主键会在对象中设置Integer delCount =
fast-el 是轻量级的高效的表达式计算引擎,源自于企业项目,设计目标是为了满足不断变化的功能需求和性能需求。 Fel 是开放的,引擎执行中的多个模块都可以扩展或替换。Fel 的执行主要是通过函数实现,运算符(+、-等都是Fel函数),所有这些函数都是可以替换的,扩展函数也非常简单。 Fel 有双引擎,同时支持解释执行和编译执行。可以根据性能要求选择执行方式。编译执行就是将表达式编译成字节码(生