xml-twig 1.3.7 → 1.3.9

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/README.md CHANGED
@@ -9,7 +9,7 @@ When you need to read a XML file, then you have two principles:
9
9
 
10
10
  * The **Document Object Model (DOM)** style. These parser read the entire XML document into memory. Usually they provide easy methods to navigate in the document tree or make modifications.
11
11
 
12
- DOM parsers are perfect for rather small files, for example configuration files or (X-)HTML pages. However, for bigger XML files you may run into memory limits.
12
+ DOM parsers are perfect for rather small files, for example configuration files or (X-)HTML pages. However, for bigger XML files you may run into memory limits. When you parse a XML-File as DOM, then the footprint in RAM can be easily 10-20 times the size of the raw XML-String. If the XML-File is greater than [Buffer.constants.MAX_STRING_LENGTH`](https://nodejs.org/api/buffer.html#bufferconstantsmax_string_length) (typically 512 MiB), then a DOM parser may throw error "Cannot create a string longer than 0x1fffffe8 characters".
13
13
 
14
14
  * The **stream** or **event** based parsers. These parser read the XML file "line by line". The biggest advantage of such a parser is, there is no limit in the size of the XML file. You can read XML files having a size of many terabytes, because you read always just a single node.
15
15
 
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  },
6
6
  "name": "xml-twig",
7
7
  "description": "Node module for processing huge XML documents in tree mode",
8
- "version": "1.3.7",
8
+ "version": "1.3.9",
9
9
  "main": "twig.js",
10
10
  "directories": {
11
11
  "doc": "doc"
@@ -0,0 +1,108 @@
1
+ var fs = require('fs');
2
+ var xml = fs.readFileSync('../SwissProt.xml', 'utf8');
3
+
4
+ console.log(`File Size: ${Math.round((xml.length / 1024 / 1024 + Number.EPSILON) * 100) / 100} MiB`);
5
+ for (const [key, value] of Object.entries(process.memoryUsage()))
6
+ console.log(` Memory usage by ${key}, ${Math.round((value / 1024 / 1024 + Number.EPSILON) * 100) / 100} MiB`)
7
+
8
+ /*
9
+ File Size of dblp.xml: 127.66 MiB
10
+ Memory usage by rss, 290.71 MiB
11
+ Memory usage by heapTotal, 137.07 MiB
12
+ Memory usage by heapUsed, 135.74 MiB
13
+ Memory usage by external, 128.53 MiB
14
+ Memory usage by arrayBuffers, 0.02 MiB
15
+
16
+ File Size of SwissProt.xml: 109.5 MiB
17
+ Memory usage by rss, 387.96 MiB
18
+ Memory usage by heapTotal, 257.09 MiB
19
+ Memory usage by heapUsed, 247.7 MiB
20
+ Memory usage by external, 110.37 MiB
21
+ Memory usage by arrayBuffers, 109.52 MiB
22
+ */
23
+
24
+
25
+ /*
26
+ const { XMLParser } = require("fast-xml-parser");
27
+ let parser = new XMLParser();
28
+ let obj = parser.parse(xml);
29
+ for (const [key, value] of Object.entries(process.memoryUsage()))
30
+ console.log(` Memory usage by ${key}, ${Math.round((value / 1024 / 1024 + Number.EPSILON) * 100) / 100} MiB`)
31
+ */
32
+ /*
33
+ Memory usage by rss, 1482.1 MiB
34
+ Memory usage by heapTotal, 1444.53 MiB
35
+ Memory usage by heapUsed, 1396.38 MiB
36
+ Memory usage by external, 0.87 MiB
37
+ Memory usage by arrayBuffers, 0.02 MiB
38
+ */
39
+
40
+ /*
41
+ const twig = require('../twig.js');
42
+ let parser = twig.createParser([{ tag: twig.Root, function: rootHandler }], { method: 'expat' })
43
+ let reader = fs.createReadStream(`../SwissProt.xml`);
44
+ reader.pipe(parser);
45
+ function rootHandler(elt) {
46
+ for (const [key, value] of Object.entries(process.memoryUsage()))
47
+ console.log(` Memory usage by ${key}, ${Math.round((value / 1024 / 1024 + Number.EPSILON) * 100) / 100} MiB`)
48
+ }
49
+ */
50
+ /*
51
+ Memory usage by rss, 8872.57 MiB
52
+ Memory usage by heapTotal, 8779.85 MiB
53
+ Memory usage by heapUsed, 8586.22 MiB
54
+ Memory usage by external, 1.15 MiB
55
+ Memory usage by arrayBuffers, 0.3 MiB
56
+ */
57
+
58
+
59
+ /*
60
+ const parse = require('xml-parser');
61
+ let obj = parse(xml);
62
+ for (const [key, value] of Object.entries(process.memoryUsage()))
63
+ console.log(` Memory usage by ${key}, ${Math.round((value / 1024 / 1024 + Number.EPSILON) * 100) / 100} MiB`)
64
+ /*
65
+ Running for ages...
66
+ */
67
+
68
+
69
+ /*
70
+ const xml2js = require('xml2js');
71
+ var parser = new xml2js.Parser();
72
+ parser.parseString(xml, function (err, result) {
73
+ for (const [key, value] of Object.entries(process.memoryUsage()))
74
+ console.log(` Memory usage by ${key}, ${Math.round((value / 1024 / 1024 + Number.EPSILON) * 100) / 100} MiB`)
75
+
76
+ console.log('Done');
77
+ });
78
+ */
79
+ /*
80
+ Memory usage by rss, 898.91 MiB
81
+ Memory usage by heapTotal, 863.84 MiB
82
+ Memory usage by heapUsed, 825.19 MiB
83
+ Memory usage by external, 0.87 MiB
84
+ Memory usage by arrayBuffers, 0.02 MiB
85
+ */
86
+ /*
87
+ const parser = require("xml-parse");
88
+ var parsedXML = parser.parse(xml);
89
+ for (const [key, value] of Object.entries(process.memoryUsage()))
90
+ console.log(` Memory usage by ${key}, ${Math.round((value / 1024 / 1024 + Number.EPSILON) * 100) / 100} MiB`)
91
+ // -> Uncaught RangeError RangeError: Maximum call stack size exceeded
92
+ */
93
+
94
+ /*
95
+ const { parseXml } = require('@rgrove/parse-xml');
96
+ let r = parseXml(xml);
97
+ for (const [key, value] of Object.entries(process.memoryUsage()))
98
+ console.log(` Memory usage by ${key}, ${Math.round((value / 1024 / 1024 + Number.EPSILON) * 100) / 100} MiB`)
99
+ */
100
+ /*
101
+ Memory usage by rss, 1943.94 MiB
102
+ Memory usage by heapTotal, 1903.21 MiB
103
+ Memory usage by heapUsed, 1854.36 MiB
104
+ Memory usage by external, 0.87 MiB
105
+ Memory usage by arrayBuffers, 0.02 MiB
106
+ */
107
+
108
+
package/twig.js CHANGED
@@ -769,6 +769,14 @@ class Twig {
769
769
  return this.root().writer(true, true).output;
770
770
  };
771
771
 
772
+ /**
773
+ * Returns XML string of the element
774
+ * @returns {string} The XML-Element as string
775
+ */
776
+ toString = function () {
777
+ return this.writer(true).toString();
778
+ };
779
+
772
780
  /**
773
781
  * Internal recursive function used by `writer()`
774
782
  * @param {external:XMLWriter} xw - The writer object