fastest XML DOM Parser for node/browser/worker
npm install txmltxml.parse(xml);.
for await loop
txml, it will be the cleanest in all environments and let you use txml.parse(xml) where xml is the string.
.parseStream in favor of transformStream
const txml = require('txml'); or in typescript import * as txml from 'txml';.
txml/txml or txml/dist/txml. This will not add the transformStream into the bundle and with that exclude the Node.js files.
) (default false)
noChildNodes to [], an empty array.
js
txml.parse();
// will return an object like:
[{
"tagName": "user",
"attributes": {
"is": "great"
},
"children": [{
"tagName": "name",
"attributes": {},
"children": [ "Tobias" ]
}, {
"tagName": "familyName",
"attributes": {},
"children": [ "Nickel" ]
}, {
"tagName": "profession",
"attributes": {},
"children": [ "Software Developer" ]
}, {
"tagName": "location",
"attributes": {},
"children": [ "Shanghai / China" ]
}
]
}];
`
$3
Same purpose of simplify, to make the data easier accessible. It is modeled after PHP
s simplexml. You can quickly access properties. However, some attributes might be lost. Also some string values can be lost. For details see Issue 19.
This method is used with the simplify parsing option.
1. tXml_DOM_Object the object to simplify.
`js
txml.simplify(txml.parse());
// will return an object like:
{
"user": {
"name": "Tobias",
"familyName": "Nickel",
"profession": "Software Developer",
"location": "Shanghai / China",
"_attributes": {
"is": "great"
}
}
}
`
$3
This version is not the same as in PHP simple_xml. But therefor, you do not lose any information. If there are attributes, you get an _attribute property, even if there is only one of a kind, it will be an array with one item, for consistent code.
$3
This method is used with the filter parameter, it is used like Array.filter. But it will traverse the entire deep tree.
1. tXml_DOM_Object the object to filter.
2. f a function that returns true if you want this elements in the result set.
`js
const dom = txml.parse(
hello
);
const styleElement = data.filter(dom, node=>node.tagName.toLowerCase() === 'style')[0];
`
$3
To find an element by ID. If you are only interested for the information on, a specific node, this is easy and fast, because not the entire xml text need to get parsed, but only the small section you are interested in.
1. xml the xml string to search in.
2. id the id of the element to find
returns return one node
$3
Find the elements with the given class, without parsing the entire xml into a tDOM. So it is very fast and convenient. returns a list of elements.
1. xml the xml string to search in.
2. className the className of the element to find
$3
1. offset optional you to set short before the first item.
usually files begin with something like ""
so the offset need to be before the first item starts so that
between that item and the offset is no "<" character.
alternatively, pass a string, containing this preamble.
2. options optional, similar to the parse methods options.
return transformStream.
`js
const xmlStream = fs.createReadStream('your.xml')
.pipe(txml.transformStream());
for await(let element of xmlStream) {
// your logic here ...
}
`
The transform stream is great, because when your logic within the processing loop is slow, the file read stream will also run slower, and not fill up the RAM memory. For a more detailed explanation read here
Changelog
- version 5.1.0
- export ./* in package.json to allow older bundlers to import sub path directly.
import { parse } from 'txml/dist/txml.mjs';
- version 5.0.1
- fix simplify empty objects (issue #24)
- version 5.0.0
- improved handling of whitespace (issue #21)
- automated build with rollup (PR #23)
- version 4.0.1
- fixed children type definition not to include number (issue #20)
- add hr to self closing tags
- new parser option keepWhitespace` (issue #21)