lua-users home
lua-l archive

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


Hello,

ltxml was my first Lua library. I use it every day in a
small personal project and it works fine for me.

Code is available from LuaForge ; comments/bug fixes/
remarks etc. are welcome.

ltxml is based on TinyXML and TinyXPath, but it is not
an automatic binding of these libraries API, I did it
"by hand" and thus I tailored it to my needs.

Unfortunately I do not have much time to continue working
on it.

Here is a small kind of documentation :
- opening a xml file

doc=xml.open("file.xml")

(alternatively, you can use xml.parse to parse XML from
a string)

"open" or "parse" return a XML document.

On a document you can call the following methods :
- doc:print() - to get a dump of the XML
- doc:add(path_to_node, xml_as_string) - adds a sub-node
whose XML representation is given as string to the node
pointed to by the path, e.g : doc:add("/tree", "<node>value<node>")
- doc:delete(path_to_node) - deletes the corresponding node
- doc:write(path_to_node, new_value) - if path_to_node points
to a text node, text value is replaced by new_value
- doc:save() - to write back changes made in the XML file
- doc:select(path) - makes a XPath request on the document, returns
a table with the corresponding nodes, e.g :

nodes=xml.parse("<A><B>hello</B><B><C>1</C></B><D>world</D></A>"):select("/A/B")

The "nodes" table will contain 2 'B' nodes.

On nodes, you can call :
- name() - to get the node name ('B' in the example above)
- text() - to get the text value ('HELLO' for first B node returned by
previous call)
- next() - to get next sibling
- expand() - returns a table of key-value pairs from children nodes ({C="1"}
for second B node returned by previous call)
- child() - return first child
- attribute(attribute_name) - returns attribute value for corresponding
attribute
- print() - dumps XML starting from node
- select(path) - XPath request starting from this node

That's all... Depending on your needs, it may be far better using a
more full-featured and more heavily developped XML library for Lua.

Cheers,
Matias.