very small and fast xml-parser in pure javascript
MIT License
A very small and probably the fastest xml parser in pure javascript.
This lib is easy to use: txml.parse(xml);
.
so, there are good reasons to give tXml.js a try.
for await
loopTry without installing online: https://tnickel.de/2017/04/02/txml-online
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
In browser you load it how ever you want. For example as tag: .
In node and browserify, run "npm install txml" in your project
and then in your script you require it by const txml = require('txml');
or in typescript import * as txml from 'txml';
.
For specially small builds using modern module bundlers like rollup or webpack you can import 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.txml.parse(`<user is='great'>
<name>Tobias</name>
<familyName>Nickel</familyName>
<profession>Software Developer</profession>
<location>Shanghai / China</location>
</user>`);
// 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" ]
}
]
}];
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.
txml.simplify(txml.parse(`<user is='great'>
<name>Tobias</name>
<familyName>Nickel</familyName>
<profession>Software Developer</profession>
<location>Shanghai / China</location>
</user>`));
// will return an object like:
{
"user": {
"name": "Tobias",
"familyName": "Nickel",
"profession": "Software Developer",
"location": "Shanghai / China",
"_attributes": {
"is": "great"
}
}
}
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.
This method is used with the filter parameter, it is used like Array.filter. But it will traverse the entire deep tree.
const dom = txml.parse(`
<html>
<head>
<style>
p { color: "red" }
</style>
</head>
<body>
<p>hello</p>
</body>
</html>`);
const styleElement = data.filter(dom, node=>node.tagName.toLowerCase() === 'style')[0];
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.
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.
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
import { parse } from 'txml/dist/txml.mjs';
hr
to self closing tagskeepWhitespace
(issue #21)Tobias Nickel German software developer in Shanghai.