tXml
A very small and probably the fastest xml parser in pure javascript.
This lib only provides one single method. xml()
- this code is about 230 lines, can be easily extended.
- this code is 0.9kb minified + gzipped.
- this code is 5 - 10 times faster than sax/xml2js and still 2-3 times faster than fast-xml-parser
- this code can running in a worker.
- this code is parsing at average the same speed as native DOMParser + potential to be faster.
- this code is easy to read and good for study.
- this code creates a domObject with minimal footprint, that is easy to traverse.
- this code has proven in different projects, like RSS reader, openStreetMap, websites.
- this code can even parse handwritten XML that contains various errors.
- this code is working in client and server.
- this code is 100% covered by unit tests.
so, there are good reasons to give tXml.js a try.
XML - features
- tags
- childTags
- text-nodes
- white-spaces
- attributes with single and double quotes
- attributes without value
- xmlComments (ignored)
- CDATA
- embedded CSS and Javascript
- HTML singleTag elements br, img, link, meta, hr
- doctype definitions
- xml namespaces
- sync API for a sync process
- getElementsById/-Class direct on the xmlString
- simplify, similar to PHP's SimpleXML
- filter, similar to underscore, as a alternative to CSS selectors
- monomorphism for fast processing and fewer if statements (a node always has tagName:'', attributes:{} and children:[])
- streamSupport ! ! !
Try Online
Try without installing online: http://tnickel.de/2017/04/02/txml-online
Installation
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 xml = require('txml');
Methods
xml (xmlString, options)
- xmlString is the XML to parse.
- options is optional
- searchId an ID of some object. that can be queried. Using this is incredible fast.
- filter a method, to filter for interesting nodes, use it like Array.filter.
- simplify to simplify the object, to an easier access.
- pos where to start parsing.
- noChildNodes array of nodes, that have no children and don't need to be closed. Default is working good for html. For example when parsing rss, the link tag is used to really provide an URL that the user can open. In html however a link text is used to bind css or other resource into the document. In HTML it does not need to get closed. so by default the noChildNodes containes the tagName 'link'. Same as 'img', 'br', 'input', 'meta', 'link'. That means: when parsing rss, it makes to set
noChildNodes
to [], an empty array.
xml(`<user is='great'>
<name>Tobias</name>
<familyName>Nickel</familyName>
<profession>Software Developer</profession>
<location>Shanghai / China</location>
</user>`);
[{
"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" ]
}
]
}];
xml.simplify (tXml_DOM_Object)
this method is used with the simplify parameter;
- tXml_DOM_Object the object to simplify.
xml.simplify(xml(`<user is='great'>
<name>Tobias</name>
<familyName>Nickel</familyName>
<profession>Software Developer</profession>
<location>Shanghai / China</location>
</user>`));
{
"user": {
"name": "Tobias",
"familyName": "Nickel",
"profession": "Software Developer",
"location": "Shanghai / China",
"_attributes": {
"is": "great"
}
}
}
xml.simplifyListLess (tXml_DOM_Object)
Same purpose of simplify, to make the data easier accessible, but 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 a list with one item, for consistent code.
xml.filter (tXml_DOM_Object, f)
This method is used with the filter parameter, it is used like Array.filter. But it will traverse the entire deep tree.
- tXml_DOM_Object the object to filter.
- f a function that returns true if you want this elements in the result set.
const dom = xml(`
<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];
xml.getElementById (xml, id)
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.
- xml the xml string to search in.
- id the id of the element to find
returns return one node
xml.getElementsByClassName (xml, className)
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.
- xml the xml string to search in.
- className the className of the element to find
xml.parseStream (stream, offset)
Useful for huge files, like OSM-world, wikipedia-dump. but also deprecated, as transformStream is much better.
- stream is the stream or fileName,
- offset requires you to set short before the first item.
Usually files begin with something like
<!DOCTYPE osm><osm>
. So the offset need to be before the first item starts, because between that item and the offset is no <
character. Alternatively, pass a string, containing this preamble. Return stream, triggers event xml
to get notified when a complete node has been parsed.
const xmlStream = fs.createReadStream('your.xml')
xml.parseStream(xmlStream);
xmlStream.on('xml', node => processNode(node));
xml.transformStream (offset)
- 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.
return transformStream.
const xmlStream = fs.createReadStream('your.xml')
.pipe(xml.transformStream());
for await(let element of xmlStream) {
}
The transform stream is better, because when your logic is slow, the file read stream will also run slower, and not fill up the RAM memory. For a more detailed explanation read here
Developer
Tobias Nickel German software developer in Shanghai.