Xml to table

From XMLTVDoc
Jump to: navigation, search

This library allows you to read in an XML file and convert it into a lua table.

The library exports two functions:

XmlToTable.ParseFile([file],[level],[Callback])

and XmlToTable.ParseString([str],[level],[Callback])

ParseFile parses text in a lua file object and ParseString parses text in a string.

Both funcitons return the xml file parsed into a table. The table consists of the root tag of the xml file, the table has a single member called "Children". "Children" will also have a single member with the name of the root tag in the XML file. The file is represented in a tree structure. Each tag in the file is represented as a table with the following members:

  • "Attrs" - the tag's attributes
  • "Children" - All of a tag's children
  • "Data" - Any text data associated with the tag

The children are represented as a table with members of the names of the child tags. Each child tag is a numerically indexed table of all the tags with that name.

If "level" and "Callback" are specified then tags at the specified level are sent to the callbak instead of being added to the returned table. The root tag is level 0. For example with the following xml file:

<root>
  <level1>data</level1>
  <level1>
    <level2>data</level2>
  </level1>
</root>

if level was set to 1 then the two "level1" tags would be sent to the callback and the result of the function would be the "root" tag only.

The callback should be of the form: Callback([name],[tag]) Where "name" is the name of the tag and "tag" is the tag table. The callback should not return anything