You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Validate XML or Parse XML to JS/JSON very fast without C/C++ based libraries and no callback
2
+
Validate XML or Parse XML to JS/JSON and vise versa rapidly without C/C++ based libraries and no callback
3
3
4
4
<pstyle="color:red;"> **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.</p>
5
5
You can use this library online (press try me button above), or as command from CLI, or in your website, or in npm repo.
@@ -27,6 +27,19 @@ You can use this library online (press try me button above), or as command from
-c : To ignore value conversion (i.e. "-3" will not be converted to number -3)
115
+
* -ns : To include namespaces (bedefault ignored)
116
+
* -a : To ignore attributes
117
+
* -c : To ignore value conversion (i.e. "-3" will not be converted to number -3)
118
+
* -v : validate before parsing
119
+
* -V : only validate
101
120
102
121
To use it **on webpage**
103
122
104
-
1. Download and include [parser.js](https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/lib/parser.js)
123
+
1. Download and include [parser.js] Or use directly from [CDN](https://cdnjs.com/libraries/fast-xml-parser)(https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/lib/parser.js)
105
124
```js
106
125
var result =parser.validate(xmlData);
107
126
if(result !==true) cnosole.log(result.err);
108
127
var jsonObj =parser.parse(xmlData);
109
128
```
110
129
111
-
Or use directly from [CDN](https://cdnjs.com/libraries/fast-xml-parser)
130
+
## JSON or JS Object to XML
131
+
132
+
```js
133
+
var Parser =require("fast-xml-parser").j2xParser;
134
+
//default options need not to set
135
+
var defaultOptions = {
136
+
attributeNamePrefix :"@_",
137
+
attrNodeName:"@", //default is false
138
+
textNodeName :"#text",
139
+
ignoreAttributes :true,
140
+
encodeHTMLchar:false,
141
+
cdataTagName:"__cdata", //default is false
142
+
cdataPositionChar:"\\c",
143
+
format:false,
144
+
indentBy:"",
145
+
supressEmptyNode:false
146
+
};
147
+
var parser =newParser(defaultOptions);
148
+
var xml =parser.parse(json_or_js_obj);
149
+
150
+
```
151
+
**OPTIONS** :
152
+
153
+
154
+
***attributeNamePrefix** : Identify attributes with this prefix otherwise treat them as a tag.
155
+
***attrNodeName**: Identify attributes when they are grouped under single property.
156
+
***ignoreAttributes** : Don't check for attributes. Treats everything as tag.
157
+
***encodeHTMLchar** : encodes values (except cdata values) when writing to XML.
158
+
***cdataTagName** : If specified, parse matching tag as CDATA
159
+
***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.
160
+
***format** : If set to true, then format the XML output.
161
+
***indentBy** : indent by this char `when` format is set to `true`
162
+
***supressEmptyNode** : If set to `true`, tags with no value (text or nested tags) are written as self closing tags.
112
163
113
164
## Comparision
114
165
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.
@@ -141,6 +192,14 @@ Installation of such libraries fails on some OS. You may require to install miss
* It can handle big file now (I have tested up to 98mb). Performance report is given above.
@@ -162,11 +221,9 @@ Installation of such libraries fails on some OS. You may require to install miss
162
221
* Few validation and parsing bugs are also fixed
163
222
164
223
165
-
Some of my other NPM pojects
166
-
-[stubmatic](https://github.com/NaturalIntelligence/Stubmatic) : A stub server to mock behaviour of HTTP(s) / REST / SOAP services. Stubbing redis is on the way.
167
-
-[fast-lorem-ipsum](https://github.com/amitguptagwl/fast-lorem-ipsum) : Generate lorem ipsum words, sentences, paragraph very quickly.
224
+
### Worth to mention
168
225
169
-
### TODO
170
-
* P2: validating XML stream data
171
-
* P2: validator cli
172
-
* P2: fast XML prettyfier
226
+
-[stubmatic](https://github.com/NaturalIntelligence/Stubmatic) : A stub server to mock behaviour of HTTP(s) / REST / SOAP services.
227
+
-**[fastify-xml-body-parser](https://github.com/NaturalIntelligence/fastify-xml-body-parser/)** : Fastify plugin / module to parse XML payload / body into JS object using fast-xml-parser.
228
+
-[fast-lorem-ipsum](https://github.com/amitguptagwl/fast-lorem-ipsum) : Generate lorem ipsum words, sentences, paragraph very quickly.
229
+
-[Grapes](https://github.com/amitguptagwl/grapes) : Flexible Regular expression engine which can be applied on char stream. (under development)
0 commit comments