Home Contents | # table.fromXML | Table | Core4 Lua Commands

## SYNOPSIS

tbl = table.fromXML(str [, map])

## DESCRIPTION

table.fromXML() parses XML data and converts it into a table.

The implementation handles only start element, end element, character data and attributes. Comments are discarded.

The resulting table has one nested table entry for each element.
Any character data of an element is put into the table's value field.
If an element has attributes, then the element's table contains a attrs field with a map of key/value pairs. The order of attributes is not saved. The attrs field does not exist if the element has no attributes.

The optional map table can be used to remap element names. If the map contains an entry for an element name, then that element name is replaced with the value found in the map.

The typical XML header <?xml version="1.0" encoding="UTF-8"?> is optional. The parser has only been tested with UTF-8 data.

## RETURN VALUE

On success, returns a single table value.

On failure, returns five values: nil, a string describing the error, the line number of the error, the column number of the error and the index into the string of the error. All indexes are 1 based.

## EXAMPLE

> >
t = table.fromXML('<Element><Item type="greeting">Hello World</Item></Element>'>) print(table.tostring(t, 2))
{ Element = { Item = { attrs = { type = "greeting" }, value = "Hello World" } } }
> >
t = table.fromXML('<a><b>Hello World</b></a>', { a="Header", b="Text"}) print(table.tostring(t, 2))
{ Header = { Text = { value = "Hello World" } } }
>
print(table.fromXML('<Element><Item type="greeting">Hello World</Item></Elem>'))
nil mismatched tag 1 52 52