fast-xml-parser 5.0.5 → 5.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +3 -2
- package/lib/fxp.d.cts +429 -0
- package/package.json +9 -5
- package/src/cli/cli.js +4 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
<small>Note: If you find missing information about particular minor version, that version must have been changed without any functional change in this library.</small>
|
|
2
2
|
|
|
3
|
+
**5.0.7 / 2025-02-25**
|
|
4
|
+
- fix (#724) typings for cjs.
|
|
5
|
+
|
|
6
|
+
**5.0.6 / 2025-02-20**
|
|
7
|
+
- fix cli output (By [Angel Delgado](https://github.com/angeld7))
|
|
8
|
+
- remove multiple JSON parsing
|
|
9
|
+
|
|
3
10
|
**5.0.5 / 2025-02-20**
|
|
4
11
|
- fix parsing of string starting with 'e' or 'E' by updating strnum
|
|
5
12
|
|
package/README.md
CHANGED
|
@@ -155,7 +155,7 @@ Bundle size
|
|
|
155
155
|
|
|
156
156
|
### Documents
|
|
157
157
|
<table>
|
|
158
|
-
<tr><td>v3</td><td>v4</td><td>v6</td></tr>
|
|
158
|
+
<tr><td>v3</td><td>v4 and v5</td><td>v6</td></tr>
|
|
159
159
|
<tr>
|
|
160
160
|
<td>
|
|
161
161
|
<a href="./docs/v3/docs.md">documents</a>
|
|
@@ -179,7 +179,8 @@ Bundle size
|
|
|
179
179
|
</tr>
|
|
180
180
|
</table>
|
|
181
181
|
|
|
182
|
-
**note**: version
|
|
182
|
+
**note**: version 6 is released with version 4 for experimental use. Based on it's demand, it'll be developed and the features can be different in final release.
|
|
183
|
+
Version 5 has the same functionalities as version 4.
|
|
183
184
|
|
|
184
185
|
## Performance
|
|
185
186
|
<small>negative means error</small>
|
package/lib/fxp.d.cts
ADDED
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
type X2jOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* Preserve the order of tags in resulting JS object
|
|
4
|
+
*
|
|
5
|
+
* Defaults to `false`
|
|
6
|
+
*/
|
|
7
|
+
preserveOrder?: boolean;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Give a prefix to the attribute name in the resulting JS object
|
|
11
|
+
*
|
|
12
|
+
* Defaults to '@_'
|
|
13
|
+
*/
|
|
14
|
+
attributeNamePrefix?: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A name to group all attributes of a tag under, or `false` to disable
|
|
18
|
+
*
|
|
19
|
+
* Defaults to `false`
|
|
20
|
+
*/
|
|
21
|
+
attributesGroupName?: false | string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The name of the next node in the resulting JS
|
|
25
|
+
*
|
|
26
|
+
* Defaults to `#text`
|
|
27
|
+
*/
|
|
28
|
+
textNodeName?: string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Whether to ignore attributes when parsing
|
|
32
|
+
*
|
|
33
|
+
* When `true` - ignores all the attributes
|
|
34
|
+
*
|
|
35
|
+
* When `false` - parses all the attributes
|
|
36
|
+
*
|
|
37
|
+
* When `Array<string | RegExp>` - filters out attributes that match provided patterns
|
|
38
|
+
*
|
|
39
|
+
* When `Function` - calls the function for each attribute and filters out those for which the function returned `true`
|
|
40
|
+
*
|
|
41
|
+
* Defaults to `true`
|
|
42
|
+
*/
|
|
43
|
+
ignoreAttributes?: boolean | (string | RegExp)[] | ((attrName: string, jPath: string) => boolean);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whether to remove namespace string from tag and attribute names
|
|
47
|
+
*
|
|
48
|
+
* Defaults to `false`
|
|
49
|
+
*/
|
|
50
|
+
removeNSPrefix?: boolean;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Whether to allow attributes without value
|
|
54
|
+
*
|
|
55
|
+
* Defaults to `false`
|
|
56
|
+
*/
|
|
57
|
+
allowBooleanAttributes?: boolean;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Whether to parse tag value with `strnum` package
|
|
61
|
+
*
|
|
62
|
+
* Defaults to `true`
|
|
63
|
+
*/
|
|
64
|
+
parseTagValue?: boolean;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Whether to parse tag value with `strnum` package
|
|
68
|
+
*
|
|
69
|
+
* Defaults to `false`
|
|
70
|
+
*/
|
|
71
|
+
parseAttributeValue?: boolean;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Whether to remove surrounding whitespace from tag or attribute value
|
|
75
|
+
*
|
|
76
|
+
* Defaults to `true`
|
|
77
|
+
*/
|
|
78
|
+
trimValues?: boolean;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Give a property name to set CDATA values to instead of merging to tag's text value
|
|
82
|
+
*
|
|
83
|
+
* Defaults to `false`
|
|
84
|
+
*/
|
|
85
|
+
cdataPropName?: false | string;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* If set, parse comments and set as this property
|
|
89
|
+
*
|
|
90
|
+
* Defaults to `false`
|
|
91
|
+
*/
|
|
92
|
+
commentPropName?: false | string;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Control how tag value should be parsed. Called only if tag value is not empty
|
|
96
|
+
*
|
|
97
|
+
* @returns {undefined|null} `undefined` or `null` to set original value.
|
|
98
|
+
* @returns {unknown}
|
|
99
|
+
*
|
|
100
|
+
* 1. Different value or value with different data type to set new value.
|
|
101
|
+
* 2. Same value to set parsed value if `parseTagValue: true`.
|
|
102
|
+
*
|
|
103
|
+
* Defaults to `(tagName, val, jPath, hasAttributes, isLeafNode) => val`
|
|
104
|
+
*/
|
|
105
|
+
tagValueProcessor?: (tagName: string, tagValue: string, jPath: string, hasAttributes: boolean, isLeafNode: boolean) => unknown;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Control how attribute value should be parsed
|
|
109
|
+
*
|
|
110
|
+
* @param attrName
|
|
111
|
+
* @param attrValue
|
|
112
|
+
* @param jPath
|
|
113
|
+
* @returns {undefined|null} `undefined` or `null` to set original value
|
|
114
|
+
* @returns {unknown}
|
|
115
|
+
*
|
|
116
|
+
* Defaults to `(attrName, val, jPath) => val`
|
|
117
|
+
*/
|
|
118
|
+
attributeValueProcessor?: (attrName: string, attrValue: string, jPath: string) => unknown;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Options to pass to `strnum` for parsing numbers
|
|
122
|
+
*
|
|
123
|
+
* Defaults to `{ hex: true, leadingZeros: true, eNotation: true }`
|
|
124
|
+
*/
|
|
125
|
+
numberParseOptions?: strnumOptions;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Nodes to stop parsing at
|
|
129
|
+
*
|
|
130
|
+
* Defaults to `[]`
|
|
131
|
+
*/
|
|
132
|
+
stopNodes?: string[];
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* List of tags without closing tags
|
|
136
|
+
*
|
|
137
|
+
* Defaults to `[]`
|
|
138
|
+
*/
|
|
139
|
+
unpairedTags?: string[];
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Whether to always create a text node
|
|
143
|
+
*
|
|
144
|
+
* Defaults to `false`
|
|
145
|
+
*/
|
|
146
|
+
alwaysCreateTextNode?: boolean;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Determine whether a tag should be parsed as an array
|
|
150
|
+
*
|
|
151
|
+
* @param tagName
|
|
152
|
+
* @param jPath
|
|
153
|
+
* @param isLeafNode
|
|
154
|
+
* @param isAttribute
|
|
155
|
+
* @returns {boolean}
|
|
156
|
+
*
|
|
157
|
+
* Defaults to `() => false`
|
|
158
|
+
*/
|
|
159
|
+
isArray?: (tagName: string, jPath: string, isLeafNode: boolean, isAttribute: boolean) => boolean;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Whether to process default and DOCTYPE entities
|
|
163
|
+
*
|
|
164
|
+
* Defaults to `true`
|
|
165
|
+
*/
|
|
166
|
+
processEntities?: boolean;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Whether to process HTML entities
|
|
170
|
+
*
|
|
171
|
+
* Defaults to `false`
|
|
172
|
+
*/
|
|
173
|
+
htmlEntities?: boolean;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Whether to ignore the declaration tag from output
|
|
177
|
+
*
|
|
178
|
+
* Defaults to `false`
|
|
179
|
+
*/
|
|
180
|
+
ignoreDeclaration?: boolean;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Whether to ignore Pi tags
|
|
184
|
+
*
|
|
185
|
+
* Defaults to `false`
|
|
186
|
+
*/
|
|
187
|
+
ignorePiTags?: boolean;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Transform tag names
|
|
191
|
+
*
|
|
192
|
+
* Defaults to `false`
|
|
193
|
+
*/
|
|
194
|
+
transformTagName?: ((tagName: string) => string) | false;
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Transform attribute names
|
|
198
|
+
*
|
|
199
|
+
* Defaults to `false`
|
|
200
|
+
*/
|
|
201
|
+
transformAttributeName?: ((attributeName: string) => string) | false;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Change the tag name when a different name is returned. Skip the tag from parsed result when false is returned.
|
|
205
|
+
* Modify `attrs` object to control attributes for the given tag.
|
|
206
|
+
*
|
|
207
|
+
* @returns {string} new tag name.
|
|
208
|
+
* @returns false to skip the tag
|
|
209
|
+
*
|
|
210
|
+
* Defaults to `(tagName, jPath, attrs) => tagName`
|
|
211
|
+
*/
|
|
212
|
+
updateTag?: (tagName: string, jPath: string, attrs: {[k: string]: string}) => string | boolean;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
type strnumOptions = {
|
|
216
|
+
hex: boolean;
|
|
217
|
+
leadingZeros: boolean,
|
|
218
|
+
skipLike?: RegExp,
|
|
219
|
+
eNotation?: boolean
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
type validationOptions = {
|
|
223
|
+
/**
|
|
224
|
+
* Whether to allow attributes without value
|
|
225
|
+
*
|
|
226
|
+
* Defaults to `false`
|
|
227
|
+
*/
|
|
228
|
+
allowBooleanAttributes?: boolean;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* List of tags without closing tags
|
|
232
|
+
*
|
|
233
|
+
* Defaults to `[]`
|
|
234
|
+
*/
|
|
235
|
+
unpairedTags?: string[];
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
type XmlBuilderOptions = {
|
|
239
|
+
/**
|
|
240
|
+
* Give a prefix to the attribute name in the resulting JS object
|
|
241
|
+
*
|
|
242
|
+
* Defaults to '@_'
|
|
243
|
+
*/
|
|
244
|
+
attributeNamePrefix?: string;
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* A name to group all attributes of a tag under, or `false` to disable
|
|
248
|
+
*
|
|
249
|
+
* Defaults to `false`
|
|
250
|
+
*/
|
|
251
|
+
attributesGroupName?: false | string;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The name of the next node in the resulting JS
|
|
255
|
+
*
|
|
256
|
+
* Defaults to `#text`
|
|
257
|
+
*/
|
|
258
|
+
textNodeName?: string;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Whether to ignore attributes when building
|
|
262
|
+
*
|
|
263
|
+
* When `true` - ignores all the attributes
|
|
264
|
+
*
|
|
265
|
+
* When `false` - builds all the attributes
|
|
266
|
+
*
|
|
267
|
+
* When `Array<string | RegExp>` - filters out attributes that match provided patterns
|
|
268
|
+
*
|
|
269
|
+
* When `Function` - calls the function for each attribute and filters out those for which the function returned `true`
|
|
270
|
+
*
|
|
271
|
+
* Defaults to `true`
|
|
272
|
+
*/
|
|
273
|
+
ignoreAttributes?: boolean | (string | RegExp)[] | ((attrName: string, jPath: string) => boolean);
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Give a property name to set CDATA values to instead of merging to tag's text value
|
|
277
|
+
*
|
|
278
|
+
* Defaults to `false`
|
|
279
|
+
*/
|
|
280
|
+
cdataPropName?: false | string;
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* If set, parse comments and set as this property
|
|
284
|
+
*
|
|
285
|
+
* Defaults to `false`
|
|
286
|
+
*/
|
|
287
|
+
commentPropName?: false | string;
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Whether to make output pretty instead of single line
|
|
291
|
+
*
|
|
292
|
+
* Defaults to `false`
|
|
293
|
+
*/
|
|
294
|
+
format?: boolean;
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* If `format` is set to `true`, sets the indent string
|
|
299
|
+
*
|
|
300
|
+
* Defaults to ` `
|
|
301
|
+
*/
|
|
302
|
+
indentBy?: string;
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Give a name to a top-level array
|
|
306
|
+
*
|
|
307
|
+
* Defaults to `undefined`
|
|
308
|
+
*/
|
|
309
|
+
arrayNodeName?: string;
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Create empty tags for tags with no text value
|
|
313
|
+
*
|
|
314
|
+
* Defaults to `false`
|
|
315
|
+
*/
|
|
316
|
+
suppressEmptyNode?: boolean;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Suppress an unpaired tag
|
|
320
|
+
*
|
|
321
|
+
* Defaults to `true`
|
|
322
|
+
*/
|
|
323
|
+
suppressUnpairedNode?: boolean;
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Don't put a value for boolean attributes
|
|
327
|
+
*
|
|
328
|
+
* Defaults to `true`
|
|
329
|
+
*/
|
|
330
|
+
suppressBooleanAttributes?: boolean;
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Preserve the order of tags in resulting JS object
|
|
334
|
+
*
|
|
335
|
+
* Defaults to `false`
|
|
336
|
+
*/
|
|
337
|
+
preserveOrder?: boolean;
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* List of tags without closing tags
|
|
341
|
+
*
|
|
342
|
+
* Defaults to `[]`
|
|
343
|
+
*/
|
|
344
|
+
unpairedTags?: string[];
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Nodes to stop parsing at
|
|
348
|
+
*
|
|
349
|
+
* Defaults to `[]`
|
|
350
|
+
*/
|
|
351
|
+
stopNodes?: string[];
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Control how tag value should be parsed. Called only if tag value is not empty
|
|
355
|
+
*
|
|
356
|
+
* @returns {undefined|null} `undefined` or `null` to set original value.
|
|
357
|
+
* @returns {unknown}
|
|
358
|
+
*
|
|
359
|
+
* 1. Different value or value with different data type to set new value.
|
|
360
|
+
* 2. Same value to set parsed value if `parseTagValue: true`.
|
|
361
|
+
*
|
|
362
|
+
* Defaults to `(tagName, val, jPath, hasAttributes, isLeafNode) => val`
|
|
363
|
+
*/
|
|
364
|
+
tagValueProcessor?: (name: string, value: unknown) => unknown;
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Control how attribute value should be parsed
|
|
368
|
+
*
|
|
369
|
+
* @param attrName
|
|
370
|
+
* @param attrValue
|
|
371
|
+
* @param jPath
|
|
372
|
+
* @returns {undefined|null} `undefined` or `null` to set original value
|
|
373
|
+
* @returns {unknown}
|
|
374
|
+
*
|
|
375
|
+
* Defaults to `(attrName, val, jPath) => val`
|
|
376
|
+
*/
|
|
377
|
+
attributeValueProcessor?: (name: string, value: unknown) => unknown;
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Whether to process default and DOCTYPE entities
|
|
381
|
+
*
|
|
382
|
+
* Defaults to `true`
|
|
383
|
+
*/
|
|
384
|
+
processEntities?: boolean;
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
oneListGroup?: boolean;
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
type ESchema = string | object | Array<string|object>;
|
|
391
|
+
|
|
392
|
+
type ValidationError = {
|
|
393
|
+
err: {
|
|
394
|
+
code: string;
|
|
395
|
+
msg: string,
|
|
396
|
+
line: number,
|
|
397
|
+
col: number
|
|
398
|
+
};
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
declare class XMLParser {
|
|
402
|
+
constructor(options?: X2jOptions);
|
|
403
|
+
parse(xmlData: string | Buffer ,validationOptions?: validationOptions | boolean): any;
|
|
404
|
+
/**
|
|
405
|
+
* Add Entity which is not by default supported by this library
|
|
406
|
+
* @param entityIdentifier {string} Eg: 'ent' for &ent;
|
|
407
|
+
* @param entityValue {string} Eg: '\r'
|
|
408
|
+
*/
|
|
409
|
+
addEntity(entityIdentifier: string, entityValue: string): void;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
declare class XMLValidator{
|
|
413
|
+
static validate(xmlData: string, options?: validationOptions): true | ValidationError;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
declare class XMLBuilder {
|
|
417
|
+
constructor(options?: XmlBuilderOptions);
|
|
418
|
+
build(jObj: any): any;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
declare namespace fxp {
|
|
422
|
+
export {
|
|
423
|
+
XMLParser,
|
|
424
|
+
XMLValidator,
|
|
425
|
+
XMLBuilder
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
export = fxp;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fast-xml-parser",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.7",
|
|
4
4
|
"description": "Validate XML, Parse XML, Build XML without C/C++ based libraries",
|
|
5
5
|
"main": "./lib/fxp.cjs",
|
|
6
6
|
"type": "module",
|
|
@@ -8,10 +8,14 @@
|
|
|
8
8
|
"types": "./src/fxp.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"import":
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./src/fxp.d.ts",
|
|
13
|
+
"default": "./src/fxp.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./lib/fxp.d.cts",
|
|
17
|
+
"default": "./lib/fxp.cjs"
|
|
18
|
+
}
|
|
15
19
|
}
|
|
16
20
|
},
|
|
17
21
|
"scripts": {
|
package/src/cli/cli.js
CHANGED
|
@@ -46,23 +46,21 @@ if (process.argv[2] === '--help' || process.argv[2] === '-h') {
|
|
|
46
46
|
|
|
47
47
|
const callback = function(xmlData) {
|
|
48
48
|
let output = '';
|
|
49
|
-
if (
|
|
50
|
-
const parser = new XMLParser(options);
|
|
51
|
-
output = parser.parse(xmlData,validate);
|
|
52
|
-
} else if (validateOnly) {
|
|
49
|
+
if (validateOnly) {
|
|
53
50
|
output = XMLValidator.validate(xmlData);
|
|
54
51
|
process.exitCode = output === true ? 0 : 1;
|
|
55
|
-
}
|
|
52
|
+
} else {
|
|
56
53
|
const parser = new XMLParser(options);
|
|
57
54
|
output = JSON.stringify(parser.parse(xmlData,validate), null, 4);
|
|
58
55
|
}
|
|
59
56
|
if (outputFileName) {
|
|
60
57
|
writeToFile(outputFileName, output);
|
|
61
58
|
} else {
|
|
62
|
-
console.log(
|
|
59
|
+
console.log(output);
|
|
63
60
|
}
|
|
64
61
|
};
|
|
65
62
|
|
|
63
|
+
|
|
66
64
|
try {
|
|
67
65
|
|
|
68
66
|
if (!fileName) {
|