libxml2 3,427 (gitlab.gnome.org/GNOME/libxml2 (https://gitlab.gnome.org/GNOME/libxml2 3,427 ), MIT; v2.15.4, 1 September
is the C library under xmllint 3,427 , lxml 3,063 , PHP's DOM, Ruby's Nokogiri and Chrome 1 's XML parser: a tree API,
SAX2, XPath 1.0, XInclude, DTD, XSD 1.0 and RELAX NG validation, and the xmlTextReader pull API, which can validate against an XSD while it streams:
/* libxml2's pull parser, validating against an XSD while it streams */
#include <libxml/xmlreader.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
xmlTextReaderPtr r = xmlReaderForFile(argv[1], NULL, 0);
if (argc > 2) xmlTextReaderSchemaValidate(r, argv[2]); /* checked as nodes go by */
double total = 0;
int ret;
while ((ret = xmlTextReaderRead(r)) == 1) {
if (xmlTextReaderNodeType(r) == XML_READER_TYPE_ELEMENT &&
xmlStrEqual(xmlTextReaderConstLocalName(r), BAD_CAST "price")) {
xmlChar *text = xmlTextReaderReadString(r);
total += atof((const char *) text);
xmlFree(text);
}
}
printf("libxml2 %s: total %.2f%s\n", xmlParserVersion, total,
argc < 3 ? "" : xmlTextReaderIsValid(r) == 1 ? ", valid" : ", INVALID");
xmlFreeTextReader(r);
return ret == 0 ? 0 : 1;
}gcc -O2 -o reader reader.c $(pkg-config --cflags --libs libxml-2.0)
./reader booknest-catalog.xml catalog.xsd
sed 's/>39.50</>-39.50</' booknest-catalog.xml > bad.xml && ./reader bad.xml catalog.xsdOutput
libxml2 21502: total 134.74, valid bad.xml:42: Schemas validity error : Element 'price': [facet 'minExclusive'] The value '-39.50' must be greater than '0'. libxml2 21502: total 55.74, INVALID
On the 58 MB sample file it peaked at 3.6 MB of memory. xmlParserVersion 21502 means 2.15.2. Nearly every Linux XML stack depends on libxml2: patch it promptly.
libxml2
libxml2 3,427 is an XML parser library. Written in C, it provides bindings to C++, PHP5, Python, Pascal, Common Lisp, and other languages.Originally developed for the GNOME project, libxml2 is now a general-purpose XML/HTML toolkit rather than a GNOME-specific dependency. It implements a DOM-like tree API, a SAX2 interface, XPath and XPointer evaluation, XInclude processing, and validation against RELAX NG and XSD schemas, among other features. Its companion project, libxslt, provides XSLT 1.0 processing.
libxml2 underlies a number of higher-level libraries in other languages, including PHP's built-in DOM and SimpleXML extensions (see DOM Reference), Python's lxml, and Ruby's Nokogiri.
| Language | C |
| Origin | Developed for the GNOME project |
| Bindings | C++, PHP, Python, Pascal, Common Lisp, and others |
| Capabilities | DOM-like tree API, SAX2 interface, XPath/XPointer, XInclude, RELAX NG and XSD validation |
| Notable users | PHP's DOM/SimpleXML extensions, Python's lxml, Ruby's Nokogiri |
| License | MIT license |
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <stdio.h>
int main() {
xmlDocPtr doc = xmlReadFile("books.xml", NULL, 0);
xmlNodePtr root = xmlDocGetRootElement(doc);
printf("Root element: %s\n", root->name);
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}