libxml2#

local xml = require 'libxml2'

The libxml2 module is, as its name suggests, a Lua binding to libxml2.

Functions#

libxml2.read_file(path)#

Read xml from path and parse into an XML document.

Important

Parsing errors are printed to the log. In some cases parsing will continue, but in others parsing will fail in which case this function. returns nil.

Parameters:

path (string)

Return type:

XMLDoc

Version History

Version

Notes

0.0.1

Added

libxml2.read_string(xml, name[, callbacks])#

Parse xml from xml. If callbacks are supplied SAX based parsing can be performed. The following fields will be called if they exist on callbacks:

Field/Function

Description

startelement

Called at an opening element with the element name, a table of attributes, the name of the current document, and the current line this element occurs on.

local function startelement(name, attrs, doc, line)

end

endelement

Called at a closing element with the element name, the name of the current document, and the current line.

local function endelement(name, doc, line)

end

Important

Parsing errors are printed to the log. In some cases parsing will continue, but in others parsing will fail in which case this function. returns nil.

Parameters:
  • xml (string) – The XML string to parse.

  • name (string) – The document name, typically a path or file name.

  • callbacks (table) – (Optional)

Return type:

XMLDoc

Version History

Version

Notes

0.0.1

Added

0.1.0

Added callbacks parameter, SAX2 parsing

Classes#

class libxml2.XMLDoc#

An XML Document.

get_root_element()#

Return the root element for this document.

Return type:

XMLNode

Version History

Version

Notes

0.0.0

Added

name()#

Return the name of this document. This is the name supplied with read_string().

Return type:

string

Version History

Version

Notes

0.0.1

Added

url()#

Return the URL of this document, or the name() if it doesn’t have one.

Return type:

string

Version History

Version

Notes

0.0.1

Added

class libxml2.XMLNode#

A node within a XMLDoc. This could be an element with children, a text node, whitespace or comment.

copy()#

Clone this node.

Return type:

XMLNode

Version History

Version

Notes

0.0.1

Added

prev()#

Return the previous sibling of this node. In other words, the node that appears before this one in its parent’s children.

This function will return nil if there is no previous sibling.

Return type:

XMLNode

Version History

Version

Notes

0.0.1

Added

next()#

Return the next sibling of this node. In other words, the node that appears after this one in its parent’s children.

This function will return nil if there is no next sibling.

Return type:

XMLNode

Version History

Version

Notes

0.0.1

Added

children()#

Return the first child of this node. The prev() and next() methods of that node can be used to traverse all child nodes.

This function will return nil if this node has no children.

Return type:

XMLNode

Version History

Version

Notes

0.0.1

Added

type()#

Return a string indicating the type of this node. One of:

  • element-node

  • attribute-node

  • text-node

  • cdata-section-node

  • entity-ref-node

  • pi-node

  • comment-node

  • document-node

  • document-type-node

  • document-fragment-node

  • notation-node

  • html-document-node

  • dtd-node

  • element-declaration

  • attribute-declaration

  • entity-declaration

  • namespace-declaration

  • xinclude-start

  • xinclude-end

  • unknown

Return type:

string

Version History

Version

Notes

0.0.1

Added

name()#

Return the name of this node. For element nodes, this will be the tag name. nil is returned if the node does not have a name.

Return type:

string

Version History

Version

Notes

0.0.1

Added

prop(name[, value])#

Return or set the value of a property (attribute) on this node.

If value is omitted, the value of name property is returned.

If value is omitted and name does not exist on this node, nil is returned.

Parameters:
  • name (string) – The property (attribute) name.

  • value (string) – (Optional) If present, the property name is set to this value. If omitted, the value of name is returned.

Return type:

string

Version History

Version

Notes

0.0.1

Added

props()#

Return a sequence of property names on this node.

Return type:

table

Version History

Version

Notes

0.0.1

Added

content([text])#

Return or set the content of this node. If text is omitted, the content is returned, otherwise the content is set to text

Return type:

string

Version History

Version

Notes

0.0.1

Added

doc()#

Return the XMLDoc this node belongs to.

Return type:

XMLDoc

Version History

Version

Notes

0.0.1

Added

line()#

Return the line number this node occurs on within the input text.

This can be used to provide informative error messages about malformed data.

Return type:

integer

Version History

Version

Notes

0.0.1

Added