Testing of nodejs memory consumption for two xml files (parse to json), one of 20 KB and another of 30 MB
| library | Memory usage | Memory release (time) |
Convert XML to JSON |
|
| 20 KB file | 31 MB file | |||
| libxmljs | 8.6 MB | 411.8 MB | 10~20 sec | No |
| xml2js | 15.1 MB | 70.8 MB | > 60 sec | Yes |
| xml-js | 12.3 MB | 420.6 MB | > 60 sec | Yes |
| xml2json | 11.4 MB | 147.9 MB | 30~40 sec | Yes |
| fast-xml-parser | 8.9 MB | 396.4 MB | 30~40 sec | Yes |
Don't convert the xml to json, you must navigate through the nodes of the xml
Options to remove blanks
const libxmljs = require('libxmljs')
const xmlDoc = libxmljs.parseXml(xml, { blanks: false })Default options
const xml2js = require('xml2js')
const parser = new xml2js.Parser()
parser.parseStringPromise(xml)
.then(console.log)
.catch(console.error)Options added
const xml2js = require('xml2js')
const parser = new xml2js.Parser({
trim: true, // Trim the whitespace at the beginning and end of text nodes
mergeAttrs: true, // Merge attributes and child elements as properties of the parent
explicitArray: false // An array is created only if there is more than one.
})
parser.parseStringPromise(xml)
.then(console.log)
.catch(console.error)Default options
const xmljs = require('xml-js')
const xmlDoc = xmljs.xml2js(xml)Options added
const xmljs = require('xml-js')
const xmlDoc = xmljs.xml2js(xml, {
ignoreComment: true,
compact: true, // Whether to produce detailed object or compact object.
trim: true, // Whether to trim whitespace characters that may exist before and after the text.
nativeType: true, // Converting text of numerals or of boolean values to native type
nativeTypeAttributes: true // Converting text of numerals or of boolean values to native type
})Default options
const xml2json = require('xml2json')
const xmlDoc = xml2json.toJson(xml)Options added
const xml2json = require('xml2json')
const xmlDoc = xml2json.toJson(xml, {
object: true, // Returns a Javascript object instead of a JSON string
coerce: true, // Converting text of numerals or of boolean values to native type
alternateTextNode: true // Changes the default textNode property from $t to _t
})Default options
const parser = require('fast-xml-parser')
const xmlDoc = parser.parse(xml)Options added
const parser = require('fast-xml-parser')
const xmlDoc = parser.parse(xml, {
attributeNamePrefix: '_', // Given string to attribute name for identification, default @_
textNodeName: '_t', // Given string to text node name for identification, default #text
ignoreAttributes: false, // Ignore attributes to be parsed
ignoreNameSpace: true, // Remove namespace string from tag and attribute names
parseAttributeValue: true // Parse the value of an attribute to float, integer, or boolean
})