fast-xml-parser 3.21.1 → 4.0.0-beta.4

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.
@@ -0,0 +1,66 @@
1
+
2
+ const defaultOptions = {
3
+ preserveOrder: false,
4
+ attributeNamePrefix: '@_',
5
+ attributesGroupName: false,
6
+ textNodeName: '#text',
7
+ ignoreAttributes: true,
8
+ removeNSPrefix: false, // remove NS from tag name or attribute name if true
9
+ allowBooleanAttributes: false, //a tag can have attributes without any value
10
+ //ignoreRootElement : false,
11
+ parseTagValue: true,
12
+ parseAttributeValue: false,
13
+ trimValues: true, //Trim string values of tag and attributes
14
+ cdataPropName: false,
15
+ numberParseOptions: {
16
+ hex: true,
17
+ leadingZeros: true
18
+ },
19
+ tagValueProcessor: function(tagName, val) {
20
+ return val;
21
+ },
22
+ attributeValueProcessor: function(attrName, val) {
23
+ return val;
24
+ },
25
+ stopNodes: [], //nested tags will not be parsed even for errors
26
+ alwaysCreateTextNode: false,
27
+ isArray: () => false,
28
+ commentPropName: false,
29
+ unpairedTags: [],
30
+ processEntities: true,
31
+ htmlEntities: false,
32
+ };
33
+
34
+ const props = [
35
+ 'preserveOrder',
36
+ 'attributeNamePrefix',
37
+ 'attributesGroupName',
38
+ 'textNodeName',
39
+ 'ignoreAttributes',
40
+ 'removeNSPrefix',
41
+ 'allowBooleanAttributes',
42
+ 'parseTagValue',
43
+ 'parseAttributeValue',
44
+ 'trimValues',
45
+ 'cdataPropName',
46
+ 'tagValueProcessor',
47
+ 'attributeValueProcessor',
48
+ 'numberParseOptions',
49
+ 'stopNodes',
50
+ 'alwaysCreateTextNode',
51
+ 'isArray',
52
+ 'commentPropName',
53
+ 'unpairedTags',
54
+ 'processEntities',
55
+ 'htmlEntities'
56
+ ];
57
+
58
+ const util = require('../util');
59
+
60
+ const buildOptions = function(options) {
61
+ return util.buildOptions(options, defaultOptions, props);
62
+ };
63
+
64
+ exports.buildOptions = buildOptions;
65
+ exports.defaultOptions = defaultOptions;
66
+ exports.props = props;
@@ -0,0 +1,498 @@
1
+ 'use strict';
2
+
3
+ const util = require('../util');
4
+ const xmlNode = require('./xmlNode');
5
+ const readDocType = require("./DocTypeReader");
6
+ const toNumber = require("strnum");
7
+
8
+ const regx =
9
+ '<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|((NAME:)?(NAME))([^>]*)>|((\\/)(NAME)\\s*>))([^<]*)'
10
+ .replace(/NAME/g, util.nameRegexp);
11
+
12
+ //const tagsRegx = new RegExp("<(\\/?[\\w:\\-\._]+)([^>]*)>(\\s*"+cdataRegx+")*([^<]+)?","g");
13
+ //const tagsRegx = new RegExp("<(\\/?)((\\w*:)?([\\w:\\-\._]+))([^>]*)>([^<]*)("+cdataRegx+"([^<]*))*([^<]+)?","g");
14
+
15
+ class OrderedObjParser{
16
+ constructor(options){
17
+ this.options = options;
18
+ this.currentNode = null;
19
+ this.tagsNodeStack = [];
20
+ this.docTypeEntities = {};
21
+ this.lastEntities = {
22
+ "amp" : { regex: /&(amp|#38|#x26);/g, val : "&"},
23
+ "apos" : { regex: /&(apos|#39|#x27);/g, val : "'"},
24
+ "gt" : { regex: /&(gt|#62|#x3E);/g, val : ">"},
25
+ "lt" : { regex: /&(lt|#60|#x3C);/g, val : "<"},
26
+ "quot" : { regex: /&(quot|#34|#x22);/g, val : "\""},
27
+ };
28
+ this.htmlEntities = {
29
+ "space": { regex: /&(nbsp|#160);/g, val: " " },
30
+ // "lt" : { regex: /&(lt|#60);/g, val: "<" },
31
+ // "gt" : { regex: /&(gt|#62);/g, val: ">" },
32
+ // "amp" : { regex: /&(amp|#38);/g, val: "&" },
33
+ // "quot" : { regex: /&(quot|#34);/g, val: "\"" },
34
+ // "apos" : { regex: /&(apos|#39);/g, val: "'" },
35
+ "cent" : { regex: /&(cent|#162);/g, val: "¢" },
36
+ "pound" : { regex: /&(pound|#163);/g, val: "£" },
37
+ "yen" : { regex: /&(yen|#165);/g, val: "¥" },
38
+ "euro" : { regex: /&(euro|#8364);/g, val: "€" },
39
+ "copyright" : { regex: /&(copy|#169);/g, val: "©" },
40
+ "reg" : { regex: /&(reg|#174);/g, val: "®" },
41
+ "inr" : { regex: /&(inr|#8377);/g, val: "₹" },
42
+ };
43
+ this.addExternalEntities = addExternalEntities;
44
+ this.parseXml = parseXml;
45
+ this.parseTextData = parseTextData;
46
+ this.resolveNameSpace = resolveNameSpace;
47
+ this.buildAttributesMap = buildAttributesMap;
48
+ this.isItStopNode = isItStopNode;
49
+ this.replaceEntitiesValue = replaceEntitiesValue;
50
+ this.readTagExp = readTagExp;
51
+ this.readStopNodeData = readStopNodeData;
52
+ }
53
+
54
+ }
55
+
56
+ function addExternalEntities(externalEntities){
57
+ const entKeys = Object.keys(externalEntities);
58
+ for (let i = 0; i < entKeys.length; i++) {
59
+ const ent = entKeys[i];
60
+ this.lastEntities[ent] = {
61
+ regex: new RegExp("&"+ent+";","g"),
62
+ val : externalEntities[ent]
63
+ }
64
+ }
65
+ }
66
+
67
+ /**
68
+ * @param {string} val
69
+ * @param {string} tagName
70
+ * @param {string} jPath
71
+ * @param {boolean} dontTrim
72
+ * @param {boolean} hasAttributes
73
+ * @param {boolean} isLeafNode
74
+ */
75
+ function parseTextData(val, tagName, jPath, dontTrim, hasAttributes, isLeafNode) {
76
+ if (val !== undefined) {
77
+ if (this.options.trimValues && !dontTrim) {
78
+ val = val.trim();
79
+ }
80
+ if(val.length > 0){
81
+ val = this.replaceEntitiesValue(val);
82
+
83
+ const newval = this.options.tagValueProcessor(tagName, val, jPath, hasAttributes, isLeafNode);
84
+ if(newval === null || newval === undefined){
85
+ //don't parse
86
+ return val;
87
+ }else if(typeof newval !== typeof val || newval !== val){
88
+ //overwrite
89
+ return newval;
90
+ }else if(this.options.trimValues){
91
+ return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
92
+ }else{
93
+ const trimmedVal = val.trim();
94
+ if(trimmedVal === val){
95
+ return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
96
+ }else{
97
+ return val;
98
+ }
99
+ }
100
+ }
101
+ }
102
+ }
103
+
104
+ function resolveNameSpace(tagname) {
105
+ if (this.options.removeNSPrefix) {
106
+ const tags = tagname.split(':');
107
+ const prefix = tagname.charAt(0) === '/' ? '/' : '';
108
+ if (tags[0] === 'xmlns') {
109
+ return '';
110
+ }
111
+ if (tags.length === 2) {
112
+ tagname = prefix + tags[1];
113
+ }
114
+ }
115
+ return tagname;
116
+ }
117
+
118
+ //TODO: change regex to capture NS
119
+ //const attrsRegx = new RegExp("([\\w\\-\\.\\:]+)\\s*=\\s*(['\"])((.|\n)*?)\\2","gm");
120
+ const attrsRegx = new RegExp('([^\\s=]+)\\s*(=\\s*([\'"])([\\s\\S]*?)\\3)?', 'gm');
121
+
122
+ function buildAttributesMap(attrStr, jPath) {
123
+ if (!this.options.ignoreAttributes && typeof attrStr === 'string') {
124
+ // attrStr = attrStr.replace(/\r?\n/g, ' ');
125
+ //attrStr = attrStr || attrStr.trim();
126
+
127
+ const matches = util.getAllMatches(attrStr, attrsRegx);
128
+ const len = matches.length; //don't make it inline
129
+ const attrs = {};
130
+ for (let i = 0; i < len; i++) {
131
+ const attrName = this.resolveNameSpace(matches[i][1]);
132
+ let oldVal = matches[i][4];
133
+ const aName = this.options.attributeNamePrefix + attrName;
134
+ if (attrName.length) {
135
+ if (oldVal !== undefined) {
136
+ if (this.options.trimValues) {
137
+ oldVal = oldVal.trim();
138
+ }
139
+ oldVal = this.replaceEntitiesValue(oldVal);
140
+ const newVal = this.options.attributeValueProcessor(attrName, oldVal, jPath);
141
+ if(newVal === null || newVal === undefined){
142
+ //don't parse
143
+ attrs[aName] = oldVal;
144
+ }else if(typeof newVal !== typeof oldVal || newVal !== oldVal){
145
+ //overwrite
146
+ attrs[aName] = newVal;
147
+ }else{
148
+ //parse
149
+ attrs[aName] = parseValue(
150
+ oldVal,
151
+ this.options.parseAttributeValue,
152
+ this.options.numberParseOptions
153
+ );
154
+ }
155
+ } else if (this.options.allowBooleanAttributes) {
156
+ attrs[aName] = true;
157
+ }
158
+ }
159
+ }
160
+ if (!Object.keys(attrs).length) {
161
+ return;
162
+ }
163
+ if (this.options.attributesGroupName) {
164
+ const attrCollection = {};
165
+ attrCollection[this.options.attributesGroupName] = attrs;
166
+ return attrCollection;
167
+ }
168
+ return attrs;
169
+ }
170
+ }
171
+
172
+ const parseXml = function(xmlData) {
173
+ xmlData = xmlData.replace(/\r\n?/g, "\n"); //TODO: remove this line
174
+ const xmlObj = new xmlNode('!xml');
175
+ let currentNode = xmlObj;
176
+ let textData = "";
177
+ let jPath = "";
178
+ for(let i=0; i< xmlData.length; i++){//for each char in XML data
179
+ const ch = xmlData[i];
180
+ if(ch === '<'){
181
+ // const nextIndex = i+1;
182
+ // const _2ndChar = xmlData[nextIndex];
183
+ if( xmlData[i+1] === '/') {//Closing Tag
184
+ const closeIndex = findClosingIndex(xmlData, ">", i, "Closing Tag is not closed.")
185
+ let tagName = xmlData.substring(i+2,closeIndex).trim();
186
+
187
+ if(this.options.removeNSPrefix){
188
+ const colonIndex = tagName.indexOf(":");
189
+ if(colonIndex !== -1){
190
+ tagName = tagName.substr(colonIndex+1);
191
+ }
192
+ }
193
+
194
+ if(currentNode){
195
+ textData = this.parseTextData(textData
196
+ , currentNode.tagname
197
+ , jPath
198
+ ,false
199
+ , currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false
200
+ , Object.keys(currentNode.child).length === 0);
201
+ if(textData !== undefined && textData !== "") currentNode.add(this.options.textNodeName, textData);
202
+ textData = "";
203
+ }
204
+
205
+ jPath = jPath.substr(0, jPath.lastIndexOf("."));
206
+
207
+ currentNode = this.tagsNodeStack.pop();//avoid recurssion, set the parent tag scope
208
+ textData = "";
209
+ i = closeIndex;
210
+ } else if( xmlData[i+1] === '?') {
211
+ i = findClosingIndex(xmlData, "?>", i, "Pi Tag is not closed.")
212
+ } else if(xmlData.substr(i + 1, 3) === '!--') {
213
+ const endIndex = findClosingIndex(xmlData, "-->", i, "Comment is not closed.")
214
+ if(this.options.commentPropName){
215
+ const comment = xmlData.substring(i + 4, endIndex - 2);
216
+
217
+ //TODO: remove repeated code
218
+ if(textData){ //store previously collected data as textNode
219
+ textData = this.parseTextData(textData
220
+ , currentNode.tagname
221
+ , jPath
222
+ ,false
223
+ , currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false
224
+ , Object.keys(currentNode.child).length === 0);
225
+
226
+ if(textData !== undefined && textData !== "") currentNode.add(this.options.textNodeName, textData);
227
+ textData = "";
228
+ }
229
+ currentNode.add(this.options.commentPropName, [ { [this.options.textNodeName] : comment } ]);
230
+ }
231
+ i = endIndex;
232
+ } else if( xmlData.substr(i + 1, 2) === '!D') {
233
+ const result = readDocType(xmlData, i);
234
+ this.docTypeEntities = result.entities;
235
+ i = result.i;
236
+ }else if(xmlData.substr(i + 1, 2) === '![') {
237
+ const closeIndex = findClosingIndex(xmlData, "]]>", i, "CDATA is not closed.") - 2;
238
+ const tagExp = xmlData.substring(i + 9,closeIndex);
239
+
240
+ if(textData){ //store previously collected data as textNode
241
+ textData = this.parseTextData(textData
242
+ , currentNode.tagname
243
+ , jPath
244
+ ,false
245
+ , currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false
246
+ , Object.keys(currentNode.child).length === 0);
247
+
248
+ if(textData !== undefined && textData !== "") currentNode.add(this.options.textNodeName, textData);
249
+ textData = "";
250
+ }
251
+
252
+ //cdata should be set even if it is 0 length string
253
+ if(this.options.cdataPropName){
254
+ // let val = this.parseTextData(tagExp, this.options.cdataPropName, jPath + "." + this.options.cdataPropName, true, false, true);
255
+ // if(!val) val = "";
256
+ currentNode.add(this.options.cdataPropName, [ { [this.options.textNodeName] : tagExp } ]);
257
+ }else{
258
+ let val = this.parseTextData(tagExp, currentNode.tagname, jPath, true, false, true);
259
+ if(!val) val = "";
260
+ currentNode.add(this.options.textNodeName, val);
261
+ }
262
+
263
+ i = closeIndex + 2;
264
+ }else {//Opening tag
265
+
266
+ let result = this.readTagExp(xmlData,i);
267
+ let tagName= result.tagName;
268
+ let tagExp = result.tagExp;
269
+ let attrExpPresent = result.attrExpPresent;
270
+ let closeIndex = result.closeIndex;
271
+
272
+ //save text as child node
273
+ if (currentNode && textData) {
274
+ if(currentNode.tagname !== '!xml'){
275
+ //when nested tag is found
276
+ textData = this.parseTextData(textData
277
+ , currentNode.tagname
278
+ , jPath
279
+ , false
280
+ , currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false
281
+ , false);
282
+ if(textData !== undefined && textData !== "") currentNode.add(this.options.textNodeName, textData);
283
+ textData = "";
284
+ }
285
+ }
286
+
287
+ if(tagName !== xmlObj.tagname){
288
+ jPath += jPath ? "." + tagName : tagName;
289
+ }
290
+
291
+ //check if last tag was unpaired tag
292
+ const lastTag = currentNode;
293
+ if(lastTag && this.options.unpairedTags.indexOf(lastTag.tagname) !== -1 ){
294
+ currentNode = this.tagsNodeStack.pop();
295
+ }
296
+
297
+ if (this.isItStopNode(this.options.stopNodes, jPath, tagName)) { //TODO: namespace
298
+ let tagContent = "";
299
+ //self-closing tag
300
+ if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){}
301
+ //boolean tag
302
+ else if(this.options.unpairedTags.indexOf(tagName) !== -1){}
303
+ //normal tag
304
+ else{
305
+ //read until closing tag is found
306
+ const result = this.readStopNodeData(xmlData, tagName, closeIndex + 1);
307
+ if(!result) throw new Error(`Unexpected end of ${tagName}`);
308
+ i = result.i;
309
+ tagContent = result.tagContent;
310
+ }
311
+
312
+ const childNode = new xmlNode(tagName);
313
+ if(tagName !== tagExp && attrExpPresent){
314
+ childNode.attributes = this.buildAttributesMap(tagExp, jPath);
315
+ }
316
+ jPath = jPath.substr(0, jPath.lastIndexOf("."));
317
+ childNode.add(this.options.textNodeName, tagContent);
318
+
319
+ currentNode.addChild(childNode);
320
+ }else{
321
+ //selfClosing tag
322
+ if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
323
+
324
+ if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
325
+ tagName = tagName.substr(0, tagName.length - 1);
326
+ tagExp = tagName;
327
+ }else{
328
+ tagExp = tagExp.substr(0, tagExp.length - 1);
329
+ }
330
+
331
+ const childNode = new xmlNode(tagName);
332
+ if(tagName !== tagExp && attrExpPresent){
333
+ childNode.attributes = this.buildAttributesMap(tagExp, jPath);
334
+ }
335
+ jPath = jPath.substr(0, jPath.lastIndexOf("."));
336
+ currentNode.addChild(childNode);
337
+ }
338
+ //opening tag
339
+ else{
340
+ const childNode = new xmlNode( tagName);
341
+ this.tagsNodeStack.push(currentNode);
342
+
343
+ if(tagName !== tagExp && attrExpPresent){
344
+ childNode.attributes = this.buildAttributesMap(tagExp, jPath);
345
+ }
346
+ currentNode.addChild(childNode);
347
+ currentNode = childNode;
348
+ }
349
+ textData = "";
350
+ i = closeIndex;
351
+ }
352
+ }
353
+ }else{
354
+ textData += xmlData[i];
355
+ }
356
+ }
357
+ return xmlObj.child;
358
+ }
359
+
360
+ const replaceEntitiesValue = function(val){
361
+ if(this.options.processEntities){
362
+ for(let entityName in this.docTypeEntities){
363
+ const entity = this.docTypeEntities[entityName];
364
+ val = val.replace( entity.regx, entity.val);
365
+ }
366
+ for(let entityName in this.lastEntities){
367
+ const entity = this.lastEntities[entityName];
368
+ val = val.replace( entity.regex, entity.val);
369
+ }
370
+ if(this.options.htmlEntities){
371
+ for(let entityName in this.htmlEntities){
372
+ const entity = this.htmlEntities[entityName];
373
+ val = val.replace( entity.regex, entity.val);
374
+ }
375
+ }
376
+ }
377
+ return val;
378
+ }
379
+ //TODO: use jPath to simplify the logic
380
+ /**
381
+ *
382
+ * @param {string[]} stopNodes
383
+ * @param {string} jPath
384
+ * @param {string} currentTagName
385
+ */
386
+ function isItStopNode(stopNodes, jPath, currentTagName){
387
+ const allNodesExp = "*." + currentTagName;
388
+ for (const stopNodePath in stopNodes) {
389
+ const stopNodeExp = stopNodes[stopNodePath];
390
+ if( allNodesExp === stopNodeExp || jPath === stopNodeExp ) return true;
391
+ }
392
+ return false;
393
+ }
394
+
395
+ /**
396
+ * Returns the tag Expression and where it is ending handling single-dobule quotes situation
397
+ * @param {string} xmlData
398
+ * @param {number} i starting index
399
+ * @returns
400
+ */
401
+ function tagExpWithClosingIndex(xmlData, i){
402
+ let attrBoundary;
403
+ let tagExp = "";
404
+ for (let index = i; index < xmlData.length; index++) {
405
+ let ch = xmlData[index];
406
+ if (attrBoundary) {
407
+ if (ch === attrBoundary) attrBoundary = "";//reset
408
+ } else if (ch === '"' || ch === "'") {
409
+ attrBoundary = ch;
410
+ } else if (ch === '>') {
411
+ return {
412
+ data: tagExp,
413
+ index: index
414
+ }
415
+ } else if (ch === '\t') {
416
+ ch = " "
417
+ }
418
+ tagExp += ch;
419
+ }
420
+ }
421
+
422
+ function findClosingIndex(xmlData, str, i, errMsg){
423
+ const closingIndex = xmlData.indexOf(str, i);
424
+ if(closingIndex === -1){
425
+ throw new Error(errMsg)
426
+ }else{
427
+ return closingIndex + str.length - 1;
428
+ }
429
+ }
430
+
431
+ function readTagExp(xmlData,i){
432
+ const result = tagExpWithClosingIndex(xmlData, i+1);
433
+ let tagExp = result.data;
434
+ const closeIndex = result.index;
435
+ const separatorIndex = tagExp.search(/\s/);
436
+ let tagName = tagExp;
437
+ let attrExpPresent = true;
438
+ if(separatorIndex !== -1){//separate tag name and attributes expression
439
+ tagName = tagExp.substr(0, separatorIndex).replace(/\s\s*$/, '');
440
+ tagExp = tagExp.substr(separatorIndex + 1);
441
+ }
442
+
443
+ if(this. options.removeNSPrefix){
444
+ const colonIndex = tagName.indexOf(":");
445
+ if(colonIndex !== -1){
446
+ tagName = tagName.substr(colonIndex+1);
447
+ attrExpPresent = tagName !== result.data.substr(colonIndex + 1);
448
+ }
449
+ }
450
+
451
+ return {
452
+ tagName: tagName,
453
+ tagExp: tagExp,
454
+ closeIndex: closeIndex,
455
+ attrExpPresent: attrExpPresent,
456
+ }
457
+ }
458
+ /**
459
+ * find paired tag for a stop node
460
+ * @param {string} xmlData
461
+ * @param {string} tagName
462
+ * @param {number} i
463
+ */
464
+ function readStopNodeData(xmlData, tagName, i){
465
+ const startIndex = i;
466
+ for (; i < xmlData.length; i++) {
467
+ if( xmlData[i] === "<" && xmlData[i+1] === "/"){
468
+ const closeIndex = findClosingIndex(xmlData, ">", i, `${tagName} is not closed`);
469
+ let closeTagName = xmlData.substring(i+2,closeIndex).trim();
470
+ if(closeTagName === tagName){
471
+ return {
472
+ tagContent: xmlData.substring(startIndex, i),
473
+ i : closeIndex
474
+ }
475
+ }
476
+ i=closeIndex;
477
+ }
478
+ }//end for loop
479
+ }
480
+
481
+ function parseValue(val, shouldParse, options) {
482
+ if (shouldParse && typeof val === 'string') {
483
+ //console.log(options)
484
+ const newval = val.trim();
485
+ if(newval === 'true' ) return true;
486
+ else if(newval === 'false' ) return false;
487
+ else return toNumber(val, options);
488
+ } else {
489
+ if (util.isExist(val)) {
490
+ return val;
491
+ } else {
492
+ return '';
493
+ }
494
+ }
495
+ }
496
+
497
+
498
+ module.exports = OrderedObjParser;
@@ -0,0 +1,56 @@
1
+ const { buildOptions} = require("./OptionsBuilder");
2
+ const OrderedObjParser = require("./OrderedObjParser");
3
+ const { prettify} = require("./node2json");
4
+ const validator = require('../validator');
5
+
6
+ class XMLParser{
7
+
8
+ constructor(options){
9
+ this.externalEntities = {};
10
+ this.options = buildOptions(options);
11
+
12
+ }
13
+ /**
14
+ * Parse XML dats to JS object
15
+ * @param {string|Buffer} xmlData
16
+ * @param {boolean|Object} validationOption
17
+ */
18
+ parse(xmlData,validationOption){
19
+ if(typeof xmlData === "string"){
20
+ }else if( xmlData.toString){
21
+ xmlData = xmlData.toString();
22
+ }else{
23
+ throw new Error("XML data is accepted in String or Bytes[] form.")
24
+ }
25
+ if( validationOption){
26
+ if(validationOption === true) validationOption = {}; //validate with default options
27
+
28
+ const result = validator.validate(xmlData, validationOption);
29
+ if (result !== true) {
30
+ throw Error( `${result.err.msg}:${result.err.line}:${result.err.col}` )
31
+ }
32
+ }
33
+ const orderedObjParser = new OrderedObjParser(this.options);
34
+ orderedObjParser.addExternalEntities(this.externalEntities);
35
+ const orderedResult = orderedObjParser.parseXml(xmlData);
36
+ if(this.options.preserveOrder || orderedResult === undefined) return orderedResult;
37
+ else return prettify(orderedResult, this.options);
38
+ }
39
+
40
+ /**
41
+ * Add Entity which is not by default supported by this library
42
+ * @param {string} key
43
+ * @param {string} value
44
+ */
45
+ addEntity(key, value){
46
+ if(value.indexOf("&") !== -1){
47
+ throw new Error("Entity value can't have '&'")
48
+ }else if(key.indexOf("&") !== -1 || key.indexOf(";") !== -1){
49
+ throw new Error("An entity must be set without '&' and ';'. Eg. use '#xD' for '&#xD;'")
50
+ }else{
51
+ this.externalEntities[key] = value;
52
+ }
53
+ }
54
+ }
55
+
56
+ module.exports = XMLParser;