# Libxml2 set of examples

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/tree.h>
- <libxml/xinclude.h>
- <libxml/xmlIO.h>

Uses:

- line 106: Type xmlDocPtr from tree.h
- line 118: Macro LIBXML_TEST_VERSION from xmlversion.h
- line 118: Function xmlRegisterInputCallbacks from xmlIO.h
- line 125: Function xmlReadMemory from parser.h
- line 135: Function xmlXIncludeProcess from xinclude.h
- line 144: Function xmlDocDump from tree.h
- line 150: Function xmlFreeDoc from tree.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 xmlNewDocNode 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/parser.h>
- <libxml/tree.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

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/parser.h>
- <libxml/tree.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

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/parser.h>
- <libxml/tree.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

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/parser.h>
- <libxml/tree.h>

Uses:

- line 45: Type xmlParserCtxtPtr from tree.h
- line 47: Type xmlDocPtr from tree.h
- line 67: Function xmlCreatePushParserCtxt from parser.h
- line 80: 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

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/parser.h>
- <libxml/tree.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/parser.h>
- <libxml/tree.h>

Uses:

- line 38: Function xmlNewDoc from tree.h
- line 39: Function xmlNewDocNode from tree.h
- line 40: Function xmlDocSetRootElement from tree.h
- line 45: Function xmlCreateIntSubset from tree.h
- line 51: Function xmlNewChild from tree.h
- line 65: Function xmlNewProp from tree.h
- line 74: Function xmlNewDocText from tree.h
- line 76: Function xmlAddChild 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/tree.h>
- <libxml/parser.h>
- <libxml/xpath.h>
- <libxml/xpathInternals.h>

Uses:

- line 39: Function xmlInitParser from parser.h
- line 80: Type xmlXPathContextPtr from xpath.h
- line 81: Type xmlXPathObjectPtr from xpath.h
- line 87: Function xmlParseFile from parser.h
- line 94: Function xmlXPathNewContext from xpath.h
- line 104: Function xmlXPathFreeContext from xpath.h
- line 110: Function xmlXPathEvalExpression from xpath.h
- line 122: Function xmlXPathFreeObject from xpath.h
- line 149: Function xmlStrdup from xmlstring.h
- line 163: Function xmlStrchr from xmlstring.h
- line 179: Function xmlXPathRegisterNs from xpathInternals.h
- line 199: Type xmlNodePtr from tree.h
- line 211: 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/tree.h>
- <libxml/parser.h>
- <libxml/xpath.h>
- <libxml/xpathInternals.h>

Uses:

- line 41: Function xmlInitParser from parser.h
- line 80: Type xmlXPathContextPtr from xpath.h
- line 81: Type xmlXPathObjectPtr from xpath.h
- line 88: Function xmlParseFile from parser.h
- line 95: Function xmlXPathNewContext from xpath.h
- line 103: Function xmlXPathEvalExpression from xpath.h
- line 106: Function xmlXPathFreeContext from xpath.h
- line 116: Function xmlXPathFreeObject from xpath.h
- line 120: Function xmlDocDump from tree.h
- line 155: 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 64: Function xmlTextReaderRead from xmlreader.h
- line 69: Function xmlFreeTextReader from xmlreader.h
- line 89: Macro LIBXML_TEST_VERSION from xmlversion.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>
- <libxml/parser.h>

Uses:

- line 31: Function xmlTextReaderConstName from xmlreader.h
- line 35: Function xmlTextReaderConstValue from xmlreader.h
- line 38: Function xmlTextReaderDepth from xmlreader.h
- line 39: Function xmlTextReaderNodeType from xmlreader.h
- line 41: Function xmlTextReaderIsEmptyElement from xmlreader.h
- line 42: Function xmlTextReaderHasValue from xmlreader.h
- line 46: Function xmlStrlen from xmlstring.h
- line 61: Type xmlTextReaderPtr from xmlreader.h
- line 69: Function xmlReaderForFile from xmlreader.h
- line 74: Function xmlTextReaderRead from xmlreader.h
- line 82: Function xmlTextReaderIsValid from xmlreader.h
- line 85: 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 50: Function xmlTextReaderRead from xmlreader.h
- line 56: Function xmlFreeTextReader from xmlreader.h
- line 62: Function xmlTextReaderCurrentDoc 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 24: Function xmlTextReaderRead from xmlreader.h
- line 34: Function xmlTextReaderCurrentDoc 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 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>
- <libxml/parser.h>

Uses:

- line 65: Type xmlTextWriterPtr from xmlwriter.h
- line 69: Function xmlNewTextWriterFilename from xmlwriter.h
- line 78: Function xmlTextWriterStartDocument from xmlwriter.h
- line 87: Function xmlTextWriterStartElement from xmlwriter.h
- line 100: Function xmlTextWriterWriteComment from xmlwriter.h
- line 117: Function xmlTextWriterWriteAttribute from xmlwriter.h
- line 136: Function xmlTextWriterWriteFormatComment from xmlwriter.h
- line 155: Function xmlTextWriterWriteFormatElement from xmlwriter.h
- line 174: Function xmlTextWriterWriteElement from xmlwriter.h
- line 193: Function xmlTextWriterEndElement from xmlwriter.h
- line 313: Function xmlTextWriterEndDocument from xmlwriter.h
- line 320: Function xmlFreeTextWriter from xmlwriter.h
- line 334: Type xmlBufferPtr from tree.h
- line 340: Function xmlBufferCreate from tree.h
- line 348: Function xmlNewTextWriterMemory from xmlwriter.h
- line 606: Function xmlBufferFree from tree.h
- line 625: Function xmlNewTextWriterDoc from xmlwriter.h
- line 856: Function xmlSaveFileEnc from tree.h
- line 873: Type xmlNodePtr from tree.h
- line 878: Macro XML_DEFAULT_VERSION from parser.h
- line 878: Function xmlNewDoc from tree.h
- line 887: Function xmlNewDocNode from tree.h
- line 894: Function xmlDocSetRootElement from tree.h
- line 897: Function xmlNewTextWriterTree from xmlwriter.h
- line 1144: Type xmlCharEncodingHandlerPtr from encoding.h
- line 1149: Function xmlFindCharEncodingHandler from encoding.h

Usage:

testWriter

Author: Alfred Mickautsch