You are looking at historical revision 35530 of this page. It may differ significantly from its current revision.

libxml2

Libxml2 is a XML C parser and toolkit with DOM, SAX and text-reader APIs.

LibXML2

Libxml2 is the XML C parser and toolkit developed for the Gnome project but usable outside of the Gnome platform), it is free software available under the MIT License. XML itself is a metalanguage to design markup languages, i.e. text language where semantic and structure are added to the content using extra 'markup' information enclosed between angle brackets. HTML is the most well-known markup language. Though the library is written in C a variety of language bindings make it available in other environments.

Author

David Ireland (djireland79 at gmail dot com)

Upstream

http://xmlsoft.org/

Egg Source Code

https://gitlab.com/maxwell79/chicken-libxml-egg

libxml

[module] libxml

Miscellaneous

attributes->string

[procedure] (attributes->string attributes) → string

Converts an attribute list to string

attributes
List of attributes
Examples

Example:

(attributes->string `(("id1" . "value1") ("id2" . "value2")))
 => " id2=\"value2\" id1=\"value1\""

DOM Parser

DOM stands for the Document Object Model; this is an API for accessing XML or HTML structured documents.

dom:is-element-node?

[procedure] (dom:is-element-node? node) → boolean

Checks if specified dom:node is a element node

node
A dom:xml-node

dom:is-text-node?

[procedure] (dom:is-text-node? node) → boolean

Checks if specified dom:node is a text node

node
A dom:xml-node

dom:is-attribute-node?

[procedure] (dom:is-attribute-node? node) → boolean

Checks if specified dom:node is an attribute node

node
A dom:xml-node

dom:parse-string

[procedure] (dom:parse-string xml-string xml-size URL encoding options) → dom:doc

Parse string using the DOM parser API

xml-string
XML string
xml-size
Size of the XML string
URL
XML URL
encoding
Encoding
options
Options

dom:parse-string-default

[procedure] (dom:parse-string-default str) → dom:doc

Parse string using the DOM parser API with default options and encoding

xml-string
XML string

dom:cleanup-parser

[constant] dom:cleanup-parser → (foreign-lambda void xmlCleanupParser)

Free the dom:doc

dom:parse-file

[procedure] (dom:parse-file filename) → dom:doc

Parse a file using the DOM parser API

filename
XML file

dom:free-doc

[procedure] (dom:free-doc) → unspecified

Free the dom:doc

dom:make-parser-context

[procedure] (dom:make-parser-context) → dom:parser-context

Create a DOM parser context

dom:read-file-with-context

[procedure] (dom:read-file-with-context) → dom:doc

Parse a XML file using the given DOM parser context

dom:is-valid?

[procedure] (dom:is-valid? ctx) → boolean

Checks if the parser context is valid after parsing a file

dom:free-parser-context

[procedure] (dom:free-parser-context) → unspecified

Free the dom:parser-context

dom:to-string

[procedure] (dom:to-string) → string

Convert a dom:node to string including the children nodes

dom:next-node

[procedure] (dom:next-node) → dom:node

Move to the next dom:node

dom:node-content

[procedure] (dom:node-content) → string

Returns the contents (text) of the dom:node

dom:node-children

[procedure] (dom:node-children) → dom:node

Returns the first child node

dom:node-name

[procedure] (dom:node-name) → dom:node

Returns the name of the dom:node

dom:is-element-name?

[procedure] (dom:is-element-name? name dom:node) → boolean

Checks if the current name of the dom:node matches the specified string

name
Name (string) to match
dom:node

dom:get-attribute

[procedure] (dom:get-attribute key dom:node) → string

Returns the attribute from the specified key

key
string
dom:node

dom:attributes

[procedure] (dom:attributes n) → Association list

Returns the complete set of XML attributes for the given node

dom:node

SAX Parser

Sometimes the DOM tree output is just too large to fit reasonably into memory. In that case (and if you don't expect to save back the XML document loaded using libxml), it's better to use the SAX interface of libxml. SAX is a callback-based interface to the parser. Before parsing, the application layer registers a customized set of callbacks which are called by the library as it progresses through the XML input.

sax:user-parser-string

[procedure] (sax:user-parser-string sax-handler user-data file-name size) → number

Parse string using SAX handler

sax-handler
user-data
file-name
size

sax:make-handler

[procedure] (sax:make-handler on-start on-end on-characters) → sax-handler

Makes a SAX handler

on-start
λ called on start of element
on-end
λ called on end of element
on-characters
λ called on start of reading characters

sax:free-handler

[procedure] (sax:free-handler sax-handler) → unspecified

Frees the SAX handler

sax-handler

Text Reader Parser

Libxml2 main API is tree based, where the parsing operation results in a document loaded completely in memory, and expose it as a tree of nodes all availble at the same time. This is very simple and quite powerful, but has the major limitation that the size of the document that can be handled is limited by the size of the memory available. Libxml2 also provide a SAX based API, but that version was designed upon one of the early expat version of SAX, SAX is also not formally defined for C. SAX basically work by registering callbacks which are called directly by the parser as it progresses through the document streams. The problem is that this programming model is relatively complex, not well standardized, cannot provide validation directly, makes entity, namespace and base processing relatively hard.

The text-reader API provides a far simpler programming model. The API acts as a cursor going forward on the document stream and stopping at each node in the way. The user's code keeps control of the progress and simply calls a read-next procedure repeatedly to progress to each node in sequence in document order. There is direct support for namespaces, xml:base, entity handling and adding DTD validation on top of it was relatively simple. This API is really close to the DOM Core specification This provides a far more standard, easy to use and powerful API than the existing SAX. Moreover integrating extension features based on the tree seems relatively easy.

In a nutshell the text-reader API provides a simpler, more standard and more extensible interface to handle large documents than the existing SAX version.

text-reader:NONE

[constant] text-reader:NONE → 0
(define text-reader:NONE 0)

text-reader:element-to-string

[procedure] (text-reader:element-to-string r) → string

Converts a text reader to string including child nodes

text-reader

text-reader:end-element-is?

[procedure] (text-reader:end-element-is? name reader) → boolean

Checks if end element is specified name

name
Element name (string)
text-reader

text-reader:start-element-is?

[procedure] (text-reader:start-element-is? name reader) → boolean

Checks if start element is specified name

name
Element name (string)
text-reader

text-reader:end-element-node?

[procedure] (text-reader:end-element-node? reader) → boolean

Checks if node is an end element

reader

text-reader:text-node?

[procedure] (text-reader:text-node? reader) → boolean

Checks for text node

reader

text-reader:element-node?

[procedure] (text-reader:element-node? reader) → boolean

Checks if node is an element

reader

text-reader:make

[procedure] (text-reader:make) → text-reader

Makes a new text-reader

text-reader:read-more

[procedure] (text-reader:read-more text-reader) → unspecified

Reads the next node in the text-reader

text-reader

text-reader:free

[procedure] (text-reader:free text-reader) → unspecified

Free the specfied text-reader

text-reader

text-reader:node-type

[procedure] (text-reader:node-type text-reader) → Node type (number)

Returns the node type

text-reader

text-reader:empty-element?

[procedure] (text-reader:empty-element? text-reader) → boolean

Checks if text-reader is empty

text-reader

text-reader:move-to-attribute

[procedure] (text-reader:move-to-attribute text-reader attribute-name) → number

Moves text-reader to the specified attribute

text-reader
attribute-name
(string)

text-reader:all-attributes

[procedure] (text-reader:all-attributes r) → list

Extracts all the attributes from the element. Attributes are placed into an association list

text-reader

text-reader:move-to-next-attribute

[procedure] (text-reader:move-to-next-attribute text-reader) → number

Moves text-reader to the next attribute

text-reader

text-reader:move-to-first-attribute

[procedure] (text-reader:move-to-first-attribute text-reader) → number

Moves text-reader to the first attribute

text-reader

text-reader:move-to-element

[procedure] (text-reader:move-to-element text-reader) → number

Moves text-reader to first element

text-reader

text-reader:next

[procedure] (text-reader:next text-reader) → number

Moves text-reader to next node

text-reader

text-reader:next-sibling

[procedure] (text-reader:next-sibling text-reader) → number

Moves text-reader to next sibling node

text-reader

text-reader:name

[procedure] (text-reader:name text-reader) → string

Returns the name of the node

text-reader

text-reader:value

[procedure] (text-reader:value text-reader) → string

Returns the value of the node

text-reader

About this egg

Author

David Ireland

Colophon

Documented by hahn.