sax vs xmlbuilder vs fast-xml-parser vs xml2js vs xmldom vs libxmljs2 vs libxmljs
XML Parsing and Manipulation Libraries Comparison
1 Year
saxxmlbuilderfast-xml-parserxml2jsxmldomlibxmljs2libxmljsSimilar Packages:
What's XML Parsing and Manipulation Libraries?

These libraries provide various functionalities for parsing, manipulating, and generating XML data in JavaScript environments. They cater to different use cases, from lightweight parsing to comprehensive XML document manipulation, making them essential tools for developers working with XML data in web applications.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
sax38,137,6961,10356 kB999 months agoISC
xmlbuilder30,345,032919-85 years agoMIT
fast-xml-parser29,763,6082,678588 kB675 days agoMIT
xml2js23,091,9644,9173.44 MB2462 years agoMIT
xmldom1,456,394378-284 years agoMIT
libxmljs2149,37837.22 MB307 months agoMIT
libxmljs74,7231,05017.7 MB69a year agoMIT
Feature Comparison: sax vs xmlbuilder vs fast-xml-parser vs xml2js vs xmldom vs libxmljs2 vs libxmljs

Performance

  • sax:

    Sax is designed for streaming parsing, allowing for efficient processing of large XML files without loading the entire document into memory. This makes it highly performant for big data applications.

  • xmlbuilder:

    Xmlbuilder's performance is generally good for generating XML, but it may not be as fast as specialized parsers for parsing tasks. Its strength lies in its intuitive API for building XML structures.

  • fast-xml-parser:

    Fast-xml-parser is optimized for speed, making it one of the fastest XML parsers available. It minimizes overhead and efficiently converts XML strings into JavaScript objects, ideal for high-performance applications.

  • xml2js:

    Xml2js is relatively fast for smaller XML documents but may not match the speed of dedicated parsers like fast-xml-parser for larger datasets. It prioritizes ease of use over raw performance.

  • xmldom:

    Xmldom provides a standard DOM interface, which may introduce some performance overhead compared to lightweight parsers. Its performance is suitable for applications that require DOM manipulation.

  • libxmljs2:

    Libxmljs2 improves upon the performance of its predecessor, libxmljs, with optimizations that enhance speed and efficiency while maintaining feature richness.

  • libxmljs:

    Libxmljs offers solid performance but may not be as fast as specialized parsers like fast-xml-parser. It provides extensive features that may introduce some overhead, making it suitable for complex XML processing rather than raw speed.

Ease of Use

  • sax:

    Sax requires a good understanding of event-driven programming, which may pose a challenge for beginners. However, once mastered, it offers powerful streaming capabilities for large XML files.

  • xmlbuilder:

    Xmlbuilder offers a fluent and intuitive API for building XML documents, making it easy for developers to create well-structured XML without extensive boilerplate code.

  • fast-xml-parser:

    Fast-xml-parser is straightforward to use, with a simple API that allows for quick parsing of XML strings. Its ease of integration makes it a popular choice for developers looking for speed without complexity.

  • xml2js:

    Xml2js is designed for simplicity, making it very user-friendly for developers who need to convert XML to JavaScript objects and vice versa without delving into complex configurations.

  • xmldom:

    Xmldom provides a familiar DOM interface, which is advantageous for developers already accustomed to working with the DOM in web development. Its usability is enhanced by its compliance with standard DOM methods.

  • libxmljs2:

    Libxmljs2 retains the complexity of libxmljs but benefits from improved documentation and community support, making it slightly easier to adopt for new users familiar with XML processing.

  • libxmljs:

    Libxmljs has a steeper learning curve due to its comprehensive feature set. While powerful, it may require more time to understand and utilize effectively, especially for new users.

Feature Set

  • sax:

    Sax is a streaming parser that provides a minimal feature set focused on event-driven parsing. It does not support advanced XML features but excels in handling large files efficiently.

  • xmlbuilder:

    Xmlbuilder is focused on XML generation and provides a fluent API for building XML documents. It does not offer parsing capabilities but excels in creating well-structured XML data.

  • fast-xml-parser:

    Fast-xml-parser focuses primarily on fast parsing and does not include advanced features like XPath or XSLT. It is best suited for applications that need quick data extraction without complex manipulation requirements.

  • xml2js:

    Xml2js provides basic features for converting XML to JavaScript objects and vice versa. It is suitable for applications that require simple serialization without the need for complex XML features.

  • xmldom:

    Xmldom supports standard DOM manipulation features, allowing for comprehensive XML document handling. It is suitable for applications that require a familiar DOM interface for XML processing.

  • libxmljs2:

    Libxmljs2 retains the extensive feature set of libxmljs while providing better performance and ongoing support. It is a good choice for projects needing advanced XML features with improved reliability.

  • libxmljs:

    Libxmljs offers a rich feature set, including XPath, XSLT, and schema validation, making it suitable for complex XML document handling and manipulation. It is ideal for applications that require extensive XML processing capabilities.

Community and Support

  • sax:

    Sax has a solid community and is well-documented, making it easy to find resources and examples. Its simplicity contributes to a lower barrier to entry for new users.

  • xmlbuilder:

    Xmlbuilder is well-documented and has a supportive community, making it easy for developers to get started with XML generation. Its intuitive API contributes to a positive user experience.

  • fast-xml-parser:

    Fast-xml-parser has a growing community and is actively maintained, providing good support through documentation and community contributions. It is suitable for developers looking for a library with ongoing updates.

  • xml2js:

    Xml2js has a large user base and extensive documentation, making it easy to find help and examples. Its popularity ensures good community support for troubleshooting and best practices.

  • xmldom:

    Xmldom is supported by a community familiar with DOM manipulation, providing adequate resources and documentation for developers. Its compliance with standard DOM methods aids in community support.

  • libxmljs2:

    Libxmljs2 benefits from a more active maintenance model and community support compared to its predecessor, making it a better choice for developers seeking reliability and updates.

  • libxmljs:

    Libxmljs has a long-standing community with extensive documentation and examples available. However, its maintenance has been inconsistent, which may affect long-term projects.

How to Choose: sax vs xmlbuilder vs fast-xml-parser vs xml2js vs xmldom vs libxmljs2 vs libxmljs
  • sax:

    Use sax for streaming XML parsing when dealing with large XML files. It operates in a non-blocking manner, allowing you to process XML data as it is read, which is efficient for memory usage and performance.

  • xmlbuilder:

    Select xmlbuilder for generating XML documents programmatically. It provides a fluent API for building XML structures, making it easy to create and manipulate XML data in a readable manner.

  • fast-xml-parser:

    Choose fast-xml-parser for high-performance XML parsing when speed is a priority. It is lightweight and designed for fast parsing of XML strings into JavaScript objects, making it ideal for applications that require quick data processing.

  • xml2js:

    Choose xml2js for a straightforward and user-friendly approach to converting XML to JavaScript objects and vice versa. It is particularly useful for applications that require simple XML serialization and deserialization without complex features.

  • xmldom:

    Use xmldom when you need a DOM-compliant XML parser that allows for standard DOM manipulation. It is suitable for applications that require a familiar DOM interface for working with XML documents.

  • libxmljs2:

    Opt for libxmljs2 if you are looking for a maintained fork of libxmljs that offers similar features with improved performance and compatibility. It is a good choice for projects that require the capabilities of libxmljs but benefit from ongoing updates.

  • libxmljs:

    Select libxmljs if you need a robust library that supports XPath, XSLT, and schema validation. It is suitable for complex XML manipulations and validations, providing a comprehensive set of features for XML document handling.

README for sax

sax js

A sax-style parser for XML and HTML.

Designed with node in mind, but should work fine in the browser or other CommonJS implementations.

What This Is

  • A very simple tool to parse through an XML string.
  • A stepping stone to a streaming HTML parser.
  • A handy way to deal with RSS and other mostly-ok-but-kinda-broken XML docs.

What This Is (probably) Not

  • An HTML Parser - That's a fine goal, but this isn't it. It's just XML.
  • A DOM Builder - You can use it to build an object model out of XML, but it doesn't do that out of the box.
  • XSLT - No DOM = no querying.
  • 100% Compliant with (some other SAX implementation) - Most SAX implementations are in Java and do a lot more than this does.
  • An XML Validator - It does a little validation when in strict mode, but not much.
  • A Schema-Aware XSD Thing - Schemas are an exercise in fetishistic masochism.
  • A DTD-aware Thing - Fetching DTDs is a much bigger job.

Regarding <!DOCTYPEs and <!ENTITYs

The parser will handle the basic XML entities in text nodes and attribute values: &amp; &lt; &gt; &apos; &quot;. It's possible to define additional entities in XML by putting them in the DTD. This parser doesn't do anything with that. If you want to listen to the ondoctype event, and then fetch the doctypes, and read the entities and add them to parser.ENTITIES, then be my guest.

Unknown entities will fail in strict mode, and in loose mode, will pass through unmolested.

Usage

var sax = require("./lib/sax"),
  strict = true, // set to false for html-mode
  parser = sax.parser(strict);

parser.onerror = function (e) {
  // an error happened.
};
parser.ontext = function (t) {
  // got some text.  t is the string of text.
};
parser.onopentag = function (node) {
  // opened a tag.  node has "name" and "attributes"
};
parser.onattribute = function (attr) {
  // an attribute.  attr has "name" and "value"
};
parser.onend = function () {
  // parser stream is done, and ready to have more stuff written to it.
};

parser.write('<xml>Hello, <who name="world">world</who>!</xml>').close();

// stream usage
// takes the same options as the parser
var saxStream = require("sax").createStream(strict, options)
saxStream.on("error", function (e) {
  // unhandled errors will throw, since this is a proper node
  // event emitter.
  console.error("error!", e)
  // clear the error
  this._parser.error = null
  this._parser.resume()
})
saxStream.on("opentag", function (node) {
  // same object as above
})
// pipe is supported, and it's readable/writable
// same chunks coming in also go out.
fs.createReadStream("file.xml")
  .pipe(saxStream)
  .pipe(fs.createWriteStream("file-copy.xml"))

Arguments

Pass the following arguments to the parser function. All are optional.

strict - Boolean. Whether or not to be a jerk. Default: false.

opt - Object bag of settings regarding string formatting. All default to false.

Settings supported:

  • trim - Boolean. Whether or not to trim text and comment nodes.
  • normalize - Boolean. If true, then turn any whitespace into a single space.
  • lowercase - Boolean. If true, then lowercase tag names and attribute names in loose mode, rather than uppercasing them.
  • xmlns - Boolean. If true, then namespaces are supported.
  • position - Boolean. If false, then don't track line/col/position.
  • strictEntities - Boolean. If true, only parse predefined XML entities (&amp;, &apos;, &gt;, &lt;, and &quot;)
  • unquotedAttributeValues - Boolean. If true, then unquoted attribute values are allowed. Defaults to false when strict is true, true otherwise.

Methods

write - Write bytes onto the stream. You don't have to do this all at once. You can keep writing as much as you want.

close - Close the stream. Once closed, no more data may be written until it is done processing the buffer, which is signaled by the end event.

resume - To gracefully handle errors, assign a listener to the error event. Then, when the error is taken care of, you can call resume to continue parsing. Otherwise, the parser will not continue while in an error state.

Members

At all times, the parser object will have the following members:

line, column, position - Indications of the position in the XML document where the parser currently is looking.

startTagPosition - Indicates the position where the current tag starts.

closed - Boolean indicating whether or not the parser can be written to. If it's true, then wait for the ready event to write again.

strict - Boolean indicating whether or not the parser is a jerk.

opt - Any options passed into the constructor.

tag - The current tag being dealt with.

And a bunch of other stuff that you probably shouldn't touch.

Events

All events emit with a single argument. To listen to an event, assign a function to on<eventname>. Functions get executed in the this-context of the parser object. The list of supported events are also in the exported EVENTS array.

When using the stream interface, assign handlers using the EventEmitter on function in the normal fashion.

error - Indication that something bad happened. The error will be hanging out on parser.error, and must be deleted before parsing can continue. By listening to this event, you can keep an eye on that kind of stuff. Note: this happens much more in strict mode. Argument: instance of Error.

text - Text node. Argument: string of text.

doctype - The <!DOCTYPE declaration. Argument: doctype string.

processinginstruction - Stuff like <?xml foo="blerg" ?>. Argument: object with name and body members. Attributes are not parsed, as processing instructions have implementation dependent semantics.

sgmldeclaration - Random SGML declarations. Stuff like <!ENTITY p> would trigger this kind of event. This is a weird thing to support, so it might go away at some point. SAX isn't intended to be used to parse SGML, after all.

opentagstart - Emitted immediately when the tag name is available, but before any attributes are encountered. Argument: object with a name field and an empty attributes set. Note that this is the same object that will later be emitted in the opentag event.

opentag - An opening tag. Argument: object with name and attributes. In non-strict mode, tag names are uppercased, unless the lowercase option is set. If the xmlns option is set, then it will contain namespace binding information on the ns member, and will have a local, prefix, and uri member.

closetag - A closing tag. In loose mode, tags are auto-closed if their parent closes. In strict mode, well-formedness is enforced. Note that self-closing tags will have closeTag emitted immediately after openTag. Argument: tag name.

attribute - An attribute node. Argument: object with name and value. In non-strict mode, attribute names are uppercased, unless the lowercase option is set. If the xmlns option is set, it will also contains namespace information.

comment - A comment node. Argument: the string of the comment.

opencdata - The opening tag of a <![CDATA[ block.

cdata - The text of a <![CDATA[ block. Since <![CDATA[ blocks can get quite large, this event may fire multiple times for a single block, if it is broken up into multiple write()s. Argument: the string of random character data.

closecdata - The closing tag (]]>) of a <![CDATA[ block.

opennamespace - If the xmlns option is set, then this event will signal the start of a new namespace binding.

closenamespace - If the xmlns option is set, then this event will signal the end of a namespace binding.

end - Indication that the closed stream has ended.

ready - Indication that the stream has reset, and is ready to be written to.

noscript - In non-strict mode, <script> tags trigger a "script" event, and their contents are not checked for special xml characters. If you pass noscript: true, then this behavior is suppressed.

Reporting Problems

It's best to write a failing test if you find an issue. I will always accept pull requests with failing tests if they demonstrate intended behavior, but it is very hard to figure out what issue you're describing without a test. Writing a test is also the best way for you yourself to figure out if you really understand the issue you think you have with sax-js.