What is libxmljs2?
libxmljs2 is a Node.js library that provides a fast and efficient way to parse, manipulate, and validate XML documents. It is a binding for the libxml2 library, which is a powerful XML parsing library written in C.
What are libxmljs2's main functionalities?
Parsing XML
This feature allows you to parse an XML string into a document object model (DOM) that you can manipulate programmatically. The code sample demonstrates how to parse a simple XML string and access the root element's name.
const libxmljs = require('libxmljs2');
const xml = '<root><child foo="bar">Content</child></root>';
const xmlDoc = libxmljs.parseXml(xml);
console.log(xmlDoc.root().name()); // 'root'
XPath Queries
This feature allows you to perform XPath queries on the parsed XML document. The code sample demonstrates how to query for a specific element using an XPath expression and retrieve its text content.
const libxmljs = require('libxmljs2');
const xml = '<root><child foo="bar">Content</child></root>';
const xmlDoc = libxmljs.parseXml(xml);
const result = xmlDoc.get('//child');
console.log(result.text()); // 'Content'
XML Validation
This feature allows you to validate an XML document against an XML Schema Definition (XSD). The code sample demonstrates how to parse an XSD and an XML document, and then validate the XML document against the XSD.
const libxmljs = require('libxmljs2');
const xsd = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="root"/></xs:schema>';
const xml = '<root></root>';
const xsdDoc = libxmljs.parseXml(xsd);
const xmlDoc = libxmljs.parseXml(xml);
const isValid = xmlDoc.validate(xsdDoc);
console.log(isValid); // true
Other packages similar to libxmljs2
xml2js
xml2js is a simple XML to JavaScript object converter. It is less feature-rich compared to libxmljs2 but is easier to use for basic XML parsing and conversion tasks. It does not support XPath queries or XML validation.
fast-xml-parser
fast-xml-parser is a fast and lightweight XML parser that converts XML to JSON and vice versa. It is designed for performance and simplicity but lacks advanced features like XPath queries and XML validation that libxmljs2 offers.
xmldom
xmldom is a DOM Level 2 compliant XML parser and serializer. It provides a similar DOM API for XML manipulation but does not include features like XPath queries or XML validation. It is useful for applications that need a standard DOM interface.
Libxmljs2
LibXML bindings for node.js
This package was forked as the original one is fairly unmaintained.
var libxmljs = require('libxmljs2');
var xml =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<root>' +
'<child foo="bar">' +
'<grandchild baz="fizbuzz">grandchild content</grandchild>' +
'</child>' +
'<sibling>with content!</sibling>' +
'</root>';
var xmlDoc = libxmljs.parseXml(xml);
var gchild = xmlDoc.get('//grandchild');
console.log(gchild.text());
var children = xmlDoc.root().childNodes();
var child = children[0];
console.log(child.attr('foo').value());
Support
API and Examples
Check out the wiki http://github.com/marudor/libxmljs2/wiki.
See the examples folder.
Installation via npm
npm install libxmljs2
Contribute
Start by checking out the open issues. Specifically the desired feature ones.
Requirements
Make sure you have met the requirements for node-gyp. You DO NOT need to manually install node-gyp; it comes bundled with node.