lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


After playing around with the BadgerFish implementation mentioned in a previous post I asked myself "how hard would it be to implement an XML parser in Lua?" This is the result, here http:// peterhi.dyndns.org/plxml/index.html

It reads well formed XML strings and creates a nested table structure that does not lose any data or structural information from the input.

<?xml version="1.0"?><doc><!-- test --><person><forename>fred</ forename><br/>smith</person><green id="1"/></doc>

Becomes

return {
  data={
    { data="version=\"1.0\"", name="xml", type="pi" },
    {
      data={
        { data="test", type="comment" },
        {
          data={
{ data={ { data="fred", type="text" } }, name="forename", type="element" },
            { data={  }, name="br", type="element" },
            { data="smith", type="text" }
          },
          name="person",
          type="element"
        },
{ attributes="id=\"1\"", data={ }, name="green", type="element" }
      },
      name="doc",
      type="element"
    }
  },
  type="root"
}

which can be walked over with a walk method to manipulate the tree. It's more fully explained, with some examples, on the web page.

Now at this point it is just a programming exercise for me, I have always meant to try and write my own XML parser and I will probably do most of the things on the to do list. Is this something that other people will be interested in, that is should I turn the code into production code with tests, performance and coverage reviews and all the usual fun and games?

--
If a pickpocket meets a saint, he sees only his pockets