Jump to content

LuaXML.zip


VicToMeyeZR
 Share

Recommended Posts

File Name: LuaXML.zip

File Submitter: VicToMeyeZR

File Submitted: 18 Jan 2010

File Category: Lua Scripts

 

Place to 2 DLL files in your root SDK folder. Place the core LUA file anywhere you want in the scripts folder.

 

Go to town. :)

LuaXML

 

- a module that maps between Lua and XML without much ado

version 1.7.2, 2009-09-06 by Gerald Franz, www.viremo.de

 

LuaXML provides a minimal set of functions for the processing of XML data in Lua. It offers a

very simple and natural mapping between the XML data format and Lua tables, which

allows one to parse XML data just using Lua's normal table access and iteration

methods: Substatements and text content is represented as array data having

numerical keys, attributes and tags use string keys. This representation makes

sure that the structure of XML data is exactly preserved in a read/write cycle.

Since version 1.7, LuaXML consists of a well-optimized portable ISO-standard C file

and a small Lua file. It is published under the same liberal licensing conditions as Lua

itself (see below). It has been successfully compiled and used under Linux, various

flavours of MS Windows, and MacOS X.

 

 

 

Example

 

-- import the LuaXML module

require("LuaXML")

-- load XML data from file "test.xml" into local table xfile

local xfile = xml.load("test.xml")

-- search for substatement having the tag "scene"

local xscene = xfile:find("scene")

-- if this substatement is found...

if xscene ~= nil then

-- ...print it to screen

print(xscene)

-- print attribute id and first substatement

print( xscene.id, xscene[1] )

-- set attribute id

xscene["id"] = "newId"

end

 

-- create a new XML object and set its tag to "root"

local x = xml.new("root")

-- append a new subordinate XML object, set its tag to "child", and its content to 123

x:append("child")[1] = 123

print(x)

 

 

 

Documentation

 

LuaXML consists of the following functions:

 

require("LuaXML")

imports the LuaXML module.

LuaXML consists of a lua file (LuaXML.lua) and normally a shared library (.dll/.so), although a static linking is possible as well. Both parts are imported by this call provided that they are found in Lua's package search path.

 

function xml.new(arg)

creates a new LuaXML object.

* param arg (optional), (1) a table to be converted to be converted to a LuaXML object, or (2) the tag of the new LuaXML object

Note that it is not mandatory to use this function in order to treat a Lua table as LuaXML object. Setting the metatable just allows the usage of a more object-oriented syntax (e.g., xmlvar:str() instead of xml.str(xmlvar) ). XML objects created by xml.load() or xml.eval() automatically offer the object-oriented syntax.

* Returns new LuaXML object

 

function xml.append(var,tag)

appends a new subordinate LuaXML object to an existing one, optionally sets tag.

* param var the parent LuaXML object

* param tag (optional) the tag of the appended LuaXML object

* Returns appended LuaXML object or nil in case of error

 

function xml.load(filename)

loads XML data from a file and returns it as table

 

* param filename the name and path of the file to be loaded

* Returns a Lua table containing the xml data in case of success or nil.

 

function xml.save(var,filename)

saves a Lua var as XML file.

 

* param var, the variable to be saved, normally a table

* param filename the filename to be used. An existing file of the same name gets overwritten.

 

function xml.eval(xmlstring)

converts an XML string to a Lua table

 

* param xmlstring the string to be converted

* Returns a Lua table containing the xml data in case of success or nil.

 

function xml.tag(var, tag)

sets or returns tag of a LuaXML object. This method is just "syntactic sugar" (using a typical Lua term)

that allows the writing of clearer code. LuaXML stores the tag value of an XML statement at table

index 0, hence it can be simply accessed or altered by var[0] or var[xml.TAG] (the latter is just a

symbolic name for the value 0). However, writing var:tag() for access or var:tag("newTag") for altering

may be more self explanatory.

 

* param var, the variable whose tag should be accessed, a LuaXML object

* param tag (optional) the new tag to be set.

* Returns the current tag as string

 

function xml.str(var, indent, tag)

converts any Lua var to an xml string.

 

* param var, the variable to be converted, normally a table

* param indent (optional) the current level of indentation for pretty output. Mainly for internal use.

* param tag (optional) the tag to be used for a table without tag. Mainly for internal use.

* Returns an XML string in case of success or nil.

 

function xml.find(var, tag, attributeKey,attributeValue)

recursively parses a Lua table for a substatement fitting to the provided tag and attribute

 

* param var, the table to be searched in.

* param tag (optional) the xml tag to be found.

* param attributeKey (optional) the exact attribute to be found.

* param attributeValue (optional) the attribute value to be found.

* Returns the first (sub-)table which matches the search condition or nil.

 

function xml.registerCode(decoded,encoded)

registers a custom code for the conversion between non-standard characters and XML character entities

 

* param decoded the character (sequence) to be used within Lua.

* param encoded the character entity to be used in XML.

* By default, only the most basic entities are known to LuaXML (” & < > ').

ANSI codes above 127 are directly converted to the XML character codes

of the same number. If more character codes are needed, they can be

registered using this function.

 

 

Click here to download this file

AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD

76561197984667096.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...