Home

Awesome

xmllpegparser

xmllpegparser is a fast XML parser who uses LPeg library.

<!-- summary -->
  1. Installation
  2. Test
  3. xmllpegparser API
    1. Parsing
    2. Entity
    3. Parser
    4. Utility
    5. Document structure (default parser)
    6. Parser structure
    7. Visitor structure
    8. Default parser limitations
  4. Licence
<!-- /summary -->

Installation

luarocks install --local https://raw.githubusercontent.com/jonathanpoelen/lua-xmllpegparser/master/xmllpegparser-2.2-0.rockspec

# or in your local directory lua-xmllpegparser

luarocks make --local xmllpegparser-2.2-0.rockspec

Test

Run ./example.lua.

./example.lua xmlfile [replaceentities]

replaceentities = anything, only to enable replacement of entities.

xmllpegparser API

Parsing

Entity

Parsers

Global parser options

Utility

Document structure (default parser)

-- pos member = index of string
document = {
  children = {
    { pos=number, parent=table or nil, text=string[, cdata=true] } or
    { pos=number, parent=table or nil, tag=string, attrs={ { name=string, value=string }, ... }, children={ ... } },
    ...
  },
  bad = { children={ ... } } -- when a closed node has no match
  preprocessor = { { pos=number, tag=string, attrs={ { name=string, value=string }, ... } },
  doctype = { pos=number, name=string, ident=string or nil, pubident=string or nil, dtd=string or nil }, -- if there is a doctype
  error = string, -- if error
  lastpos = number, -- last known position of parse()
  entities = { { pos=number, name=string, value=string }, ... },
  tentities = { name=value, ... } -- only if subEntities = true
}

Parser structure

{
  parse = function(xmlstring, visitorInitArgs...) ... end,
  parseFile = function(filename, visitorInitArgs...) ... end,
  __call = function(xmlstring, visitorInitArgs...) ... end,
}

Visitor structure

Each member is optionnal.

{
  withPos = bool -- indicates if pos parameter exists in function parameter (except `finish`)
  init = function(...), -- called before parsing, returns the position of the beginning of match or nil
  finish = function(err, pos, xmlstring), -- called after parsing, returns (doc, err) or nil
  proc = function(pos, name, attrs), -- for `<?...?>`
  entity = function(pos, name, value),
  doctype = function(pos, name, ident, pubident, dtd), -- called after all entity()
  accuattr = function(table, name, value), -- `table` is an accumulator that will be transmitted to tag.attrs. Set to `false` for disable this function.
                                           -- If `nil` and `tag` is `not nil`, a default accumalator is used.
                                           -- If `false`, the accumulator is disabled.
                                           -- (`tag(pos, name, accuattr(accuattr({}, attr1, value1), attr2, value2)`)
  tag = function(pos, name, attrs), -- for a new tag (`<a>` or `<a/>`)
  open = function(), -- only for a open node (`<a>` not `<a/>`), called after `tag`.
  close = function(name),
  text = function(pos, text),
  cdata = function(pos, text), -- or `text` if nil
  comment = function(str)
}

Default parser limitations

Licence

MIT license

<!-- https://github.com/jonathanpoelen/lua-xmllpegparser -->