#

## Libxml2 set of examples

Examples Menu

- Home
- Developer Menu
- API Menu
- InputOutput Examples
- Parsing Examples
- Tree Examples
- XPath Examples
- xmlReader Examples
- xmlWriter Examples
- XML Guidelines

Related links

- Mail archive
- XSLT libxslt
- DOM gdome2
- XML-DSig xmlsec
- FTP
- Windows binaries
- Solaris binaries
- MacOsX binaries
- lxml Python bindings
- Perl bindings
- C++ bindings
- PHP bindings
- Pascal bindings
- Ruby bindings
- Tcl bindings
- Bug Tracker

The examples are stored per section depending on the main focus of the example:

- xmlWriter :

  - testWriter.c: use various APIs for the xmlWriter

- InputOutput :

  - io1.c: Example of custom Input/Output
  - io2.c: Output to char buffer

- Tree :

  - tree1.c: Navigates a tree to print element names
  - tree2.c: Creates a tree

- XPath :

  - xpath1.c: Evaluate XPath expression and prints result node set.
  - xpath2.c: Load a document, locate subelements with XPath, modify said elements and save the resulting document.

- Parsing :

  - parse1.c: Parse an XML file to a tree and free it
  - parse2.c: Parse and validate an XML file to a tree and free the result
  - parse3.c: Parse an XML document in memory to a tree and free it
  - parse4.c: Parse an XML document chunk by chunk to a tree and free it

- xmlReader :

  - reader1.c: Parse an XML file with an xmlReader
  - reader2.c: Parse and validate an XML file with an xmlReader
  - reader3.c: Show how to extract subdocuments with xmlReader
  - reader4.c: Parse multiple XML files reusing an xmlReader

Getting the compilation options and libraries dependencies needed to generate binaries from the examples is best done on Linux/Unix by using the xml2-config script which should have been installed as part of make install step or when installing the libxml2 development package:

    gcc -o example `xml2-config --cflags` example.c `xml2-config --libs`

## InputOutput Examples

### io1.c: Example of custom Input/Output

Demonstrate the use of xmlRegisterInputCallbacks to build a custom I/O layer, this is used in an XInclude method context to show how dynamic document can be built in a clean way.

Includes:

- <libxml/parser.h>
- <libxml/xmlIO.h>
- <libxml/xinclude.h>
- <libxml/tree.h>

Uses:

- line 105: Type xmlDocPtr from tree.h
- line 117: Macro LIBXML_TEST_VERSION from xmlversion.h
- line 117: Function xmlRegisterInputCallbacks from xmlIO.h
- line 124: Function xmlReadMemory from parser.h
- line 134: Function xmlXIncludeProcess from xinclude.h
- line 143: Function xmlDocDump from tree.h
- line 149: Function xmlFreeDoc from tree.h
- line 154: Function xmlCleanupParser from parser.h
- line 158: Function xmlMemoryDump from xmlmemory.h

Usage:

io1

Author: Daniel Veillard

### io2.c: Output to char buffer

Demonstrate the use of xmlDocDumpMemory to output document to a character buffer

Includes:

- <libxml/parser.h>

Uses:

- line 19: Type xmlNodePtr from tree.h
- line 20: Type xmlDocPtr from tree.h
- line 27: Function xmlNewDoc from tree.h
- line 28: Function xmlNewNode from tree.h
- line 29: Function xmlNodeSetContent from tree.h
- line 30: Function xmlDocSetRootElement from tree.h
- line 36: Function xmlDocDumpFormatMemory from tree.h
- line 43: Function xmlFreeDoc from tree.h

Usage:

io2

Author: John Fleck

## Parsing Examples

### parse1.c: Parse an XML file to a tree and free it

Demonstrate the use of xmlReadFile() to read an XML file into a tree and xmlFreeDoc() to free the resulting tree

Includes:

- <libxml/tree.h>
- <libxml/parser.h>

Uses:

- line 24: Type xmlDocPtr from tree.h
- line 26: Function xmlReadFile from parser.h
- line 31: Function xmlFreeDoc from tree.h
- line 45: Macro LIBXML_TEST_VERSION from xmlversion.h
- line 50: Function xmlCleanupParser from parser.h
- line 54: Function xmlMemoryDump from xmlmemory.h

Usage:

parse1 test1.xml

Author: Daniel Veillard

### parse2.c: Parse and validate an XML file to a tree and free the result

Create a parser context for an XML file, then parse and validate the file, creating a tree, check the validation result and xmlFreeDoc() to free the resulting tree.

Includes:

- <libxml/tree.h>
- <libxml/parser.h>

Uses:

- line 25: Type xmlParserCtxtPtr from tree.h
- line 26: Type xmlDocPtr from tree.h
- line 29: Function xmlNewParserCtxt from parser.h
- line 35: Function xmlCtxtReadFile from parser.h
- line 44: Function xmlFreeDoc from tree.h
- line 47: Function xmlFreeParserCtxt from parser.h
- line 61: Macro LIBXML_TEST_VERSION from xmlversion.h
- line 66: Function xmlCleanupParser from parser.h
- line 70: Function xmlMemoryDump from xmlmemory.h

Usage:

parse2 test2.xml

Author: Daniel Veillard

### parse3.c: Parse an XML document in memory to a tree and free it

Demonstrate the use of xmlReadMemory() to read an XML file into a tree and xmlFreeDoc() to free the resulting tree

Includes:

- <libxml/tree.h>
- <libxml/parser.h>

Uses:

- line 27: Type xmlDocPtr from tree.h
- line 33: Function xmlReadMemory from parser.h
- line 38: Function xmlFreeDoc from tree.h
- line 49: Macro LIBXML_TEST_VERSION from xmlversion.h
- line 54: Function xmlCleanupParser from parser.h
- line 58: Function xmlMemoryDump from xmlmemory.h

Usage:

parse3

Author: Daniel Veillard

### parse4.c: Parse an XML document chunk by chunk to a tree and free it

Demonstrate the use of xmlCreatePushParserCtxt() and xmlParseChunk() to read an XML file progressively into a tree and xmlFreeDoc() to free the resulting tree

Includes:

- <libxml/tree.h>
- <libxml/parser.h>

Uses:

- line 45: Type xmlParserCtxtPtr from tree.h
- line 47: Type xmlDocPtr from tree.h
- line 67: Function xmlCreatePushParserCtxt from parser.h
- line 86: Function xmlParseChunk from parser.h
- line 94: Function xmlFreeParserCtxt from parser.h
- line 103: Function xmlFreeDoc from tree.h
- line 120: Macro LIBXML_TEST_VERSION from xmlversion.h
- line 131: Function xmlCleanupParser from parser.h
- line 135: Function xmlMemoryDump from xmlmemory.h

Usage:

parse4 test3.xml

Author: Daniel Veillard

## Tree Examples

### tree1.c: Navigates a tree to print element names

Parse a file to a tree, use xmlDocGetRootElement() to get the root element, then walk the document and print all the element name in document order.

Includes:

- <libxml/tree.h>
- <libxml/parser.h>

Uses:

- line 67: Function xmlReadFile from parser.h
- line 74: Function xmlDocGetRootElement from tree.h

Usage:

tree1 filename_or_URL

Author: Dodji Seketeli

### tree2.c: Creates a tree

Shows how to create document, nodes and dump it to stdout or file.

Includes:

- <libxml/tree.h>
- <libxml/parser.h>

Uses:

- line 38: Function xmlNewDoc from tree.h
- line 40: Function xmlDocSetRootElement from tree.h
- line 45: Function xmlCreateIntSubset from tree.h
- line 73: Function xmlNewNode from tree.h
- line 74: Function xmlNewText from tree.h
- line 77: Function xmlAddChild from tree.h
- line 87: Function xmlNewChild from tree.h
- line 88: Function xmlNewProp from tree.h
- line 95: Function xmlSaveFormatFileEnc from tree.h

Usage:

tree2 <filename> -Default output: stdout

Author: Lucas Brasilino <brasilino@recife.pe.gov.br>

## XPath Examples

### xpath1.c: Evaluate XPath expression and prints result node set.

Shows how to evaluate XPath expression and register known namespaces in XPath context.

Includes:

- <libxml/parser.h>
- <libxml/xpath.h>
- <libxml/xpathInternals.h>
- <libxml/tree.h>

Uses:

- line 39: Function xmlInitParser from parser.h
- line 87: Type xmlXPathContextPtr from xpath.h
- line 88: Type xmlXPathObjectPtr from xpath.h
- line 94: Function xmlParseFile from parser.h
- line 101: Function xmlXPathNewContext from xpath.h
- line 117: Function xmlXPathEvalExpression from xpath.h
- line 129: Function xmlXPathFreeObject from xpath.h
- line 130: Function xmlXPathFreeContext from xpath.h
- line 156: Function xmlStrdup from xmlstring.h
- line 180: Function xmlStrchr from xmlstring.h
- line 186: Function xmlXPathRegisterNs from xpathInternals.h
- line 206: Type xmlNodePtr from tree.h
- line 218: Type xmlNsPtr from tree.h

Usage:

xpath1 <xml-file> <xpath-expr> [<known-ns-list>]

Author: Aleksey Sanin

### xpath2.c: Load a document, locate subelements with XPath, modify said elements and save the resulting document.

Shows how to make a full round-trip from a load/edit/save

Includes:

- <libxml/parser.h>
- <libxml/xpath.h>
- <libxml/xpathInternals.h>
- <libxml/tree.h>

Uses:

- line 41: Function xmlInitParser from parser.h
- line 87: Type xmlXPathContextPtr from xpath.h
- line 88: Type xmlXPathObjectPtr from xpath.h
- line 95: Function xmlParseFile from parser.h
- line 102: Function xmlXPathNewContext from xpath.h
- line 110: Function xmlXPathEvalExpression from xpath.h
- line 123: Function xmlXPathFreeObject from xpath.h
- line 124: Function xmlXPathFreeContext from xpath.h
- line 127: Function xmlDocDump from tree.h
- line 162: Function xmlNodeSetContent from tree.h

Usage:

xpath2 <xml-file> <xpath-expr> <new-value>

Author: Aleksey Sanin and Daniel Veillard

## xmlReader Examples

### reader1.c: Parse an XML file with an xmlReader

Demonstrate the use of xmlReaderForFile() to parse an XML file and dump the information about the nodes found in the process. (Note that the XMLReader functions require libxml2 version later than 2.6.)

Includes:

- <libxml/xmlreader.h>

Uses:

- line 29: Function xmlTextReaderConstName from xmlreader.h
- line 33: Function xmlTextReaderConstValue from xmlreader.h
- line 36: Function xmlTextReaderDepth from xmlreader.h
- line 37: Function xmlTextReaderNodeType from xmlreader.h
- line 39: Function xmlTextReaderIsEmptyElement from xmlreader.h
- line 40: Function xmlTextReaderHasValue from xmlreader.h
- line 44: Function xmlStrlen from xmlstring.h
- line 59: Type xmlTextReaderPtr from xmlreader.h
- line 62: Function xmlReaderForFile from xmlreader.h
- line 67: Function xmlTextReaderRead from xmlreader.h
- line 69: Function xmlFreeTextReader from xmlreader.h
- line 89: Macro LIBXML_TEST_VERSION from xmlversion.h
- line 94: Function xmlCleanupParser from parser.h
- line 98: Function xmlMemoryDump from xmlmemory.h

Usage:

reader1 <filename>

Author: Daniel Veillard

### reader2.c: Parse and validate an XML file with an xmlReader

Demonstrate the use of xmlReaderForFile() to parse an XML file validating the content in the process and activating options like entities substitution, and DTD attributes defaulting. (Note that the XMLReader functions require libxml2 version later than 2.6.)

Includes:

- <libxml/xmlreader.h>

Uses:

- line 30: Function xmlTextReaderConstName from xmlreader.h
- line 34: Function xmlTextReaderConstValue from xmlreader.h
- line 37: Function xmlTextReaderDepth from xmlreader.h
- line 38: Function xmlTextReaderNodeType from xmlreader.h
- line 40: Function xmlTextReaderIsEmptyElement from xmlreader.h
- line 41: Function xmlTextReaderHasValue from xmlreader.h
- line 45: Function xmlStrlen from xmlstring.h
- line 60: Type xmlTextReaderPtr from xmlreader.h
- line 68: Function xmlReaderForFile from xmlreader.h
- line 76: Function xmlTextReaderRead from xmlreader.h
- line 81: Function xmlTextReaderIsValid from xmlreader.h
- line 84: Function xmlFreeTextReader from xmlreader.h

Usage:

reader2 <valid_xml_filename>

Author: Daniel Veillard

### reader3.c: Show how to extract subdocuments with xmlReader

Demonstrate the use of xmlTextReaderPreservePattern() to parse an XML file with the xmlReader while collecting only some subparts of the document. (Note that the XMLReader functions require libxml2 version later than 2.6.)

Includes:

- <libxml/xmlreader.h>

Uses:

- line 32: Type xmlTextReaderPtr from xmlreader.h
- line 38: Function xmlReaderForFile from xmlreader.h
- line 43: Function xmlTextReaderPreservePattern from xmlreader.h
- line 52: Function xmlTextReaderRead from xmlreader.h
- line 62: Function xmlTextReaderCurrentDoc from xmlreader.h
- line 66: Function xmlFreeTextReader from xmlreader.h
- line 96: Function xmlDocDump from tree.h

Usage:

reader3

Author: Daniel Veillard

### reader4.c: Parse multiple XML files reusing an xmlReader

Demonstrate the use of xmlReaderForFile() and xmlReaderNewFile to parse XML files while reusing the reader object and parser context. (Note that the XMLReader functions require libxml2 version later than 2.6.)

Includes:

- <libxml/xmlreader.h>

Uses:

- line 26: Function xmlTextReaderRead from xmlreader.h
- line 54: Type xmlTextReaderPtr from xmlreader.h
- line 72: Function xmlReaderForFile from xmlreader.h
- line 83: Function xmlReaderNewFile from xmlreader.h
- line 97: Function xmlTextReaderCurrentDoc from xmlreader.h
- line 104: Function xmlFreeTextReader from xmlreader.h

Usage:

reader4 <filename> [ filename ... ]

Author: Graham Bennett

## xmlWriter Examples

### testWriter.c: use various APIs for the xmlWriter

tests a number of APIs for the xmlWriter, especially the various methods to write to a filename, to a memory buffer, to a new document, or to a subtree. It shows how to do encoding string conversions too. The resulting documents are then serialized.

Includes:

- <libxml/encoding.h>
- <libxml/xmlwriter.h>

Uses:

- line 76: Function xmlNewTextWriterFilename from xmlwriter.h
- line 341: Type xmlBufferPtr from tree.h
- line 347: Function xmlBufferCreate from tree.h
- line 355: Function xmlNewTextWriterMemory from xmlwriter.h
- line 613: Function xmlBufferFree from tree.h
- line 632: Function xmlNewTextWriterDoc from xmlwriter.h
- line 878: Type xmlTextWriterPtr from xmlwriter.h
- line 880: Type xmlNodePtr from tree.h
- line 885: Macro XML_DEFAULT_VERSION from parser.h
- line 885: Function xmlNewDoc from tree.h
- line 894: Function xmlNewDocNode from tree.h
- line 901: Function xmlDocSetRootElement from tree.h
- line 904: Function xmlNewTextWriterTree from xmlwriter.h
- line 913: Function xmlTextWriterStartDocument from xmlwriter.h
- line 925: Function xmlTextWriterWriteComment from xmlwriter.h
- line 949: Function xmlTextWriterWriteAttribute from xmlwriter.h
- line 959: Function xmlTextWriterWriteFormatComment from xmlwriter.h
- line 1073: Function xmlTextWriterWriteFormatElement from xmlwriter.h
- line 1096: Function xmlTextWriterStartElement from xmlwriter.h
- line 1103: Function xmlTextWriterWriteElement from xmlwriter.h
- line 1111: Function xmlTextWriterEndElement from xmlwriter.h
- line 1121: Function xmlTextWriterEndDocument from xmlwriter.h
- line 1127: Function xmlFreeTextWriter from xmlwriter.h
- line 1129: Function xmlSaveFileEnc from tree.h
- line 1151: Type xmlCharEncodingHandlerPtr from encoding.h
- line 1156: Function xmlFindCharEncodingHandler from encoding.h

Usage:

testWriter

Author: Alfred Mickautsch

Daniel Veillard