Monday 26 November 2007

XML parser demonstration




Video on YouTube.

I managed to get the parser to handle attributes and tags with attributes.
xml.stag is no longer necessary and has been replaced with the more vesatile xml.tag .
Above is an approximately 8 minute video demonstrating use of the parser within the Lua interpreter. I apologise for the poor quality.

The next step for the parser now is to implement handling a specific occurrence of tag for the specified functions.

This video is licenced under same licence as the rest of this blog contents, except third party stuff of course (such as featured images).

Digg This Story

Tuesday 20 November 2007

Lua XML Parser - so far, so good.


California XML licence plate.

As mentioned in my last posting, I have been developing a simple XML parser for use with the Lua programming languge. It will most likely be a very basic parser, that is not one on terms with more advanced parsers.

So far I have implemented 2 basic functions for my parser:


  1. Loading XML documents

  2. Parsing short tags (e.g. Something like <tag>value</tag>)



To include the functionality of the parser (which is a Lua module) into a new Lua program, I include the line:


xml = require "xmlparser"



This gives the program access to the two functions (I have so far) provided by my parser.

These are:


xml.load(file as str)
xml.stag(xml as str, tag as str [, occur as int])


I have written this simple program to test my parser as it stands so far:


--------------------------------------------------
-- Example program using XML Parser
-- Copyright (c) 2007 Sam Saint-Pettersen
-- Released under the GNU General Public License
-- http://creativecommons.org/licenses/GPL/2.0/
--------------------------------------------------

-- include xmlparser module
xml = require "xmlparser"

-- load xml file
file = xml.load("employees.xml")

-- display name
print("Name: " .. xml.stag(file, "last") .. ", " ..
xml.stag(file, "first")) -- < this should be on above line
-- display position
print("Position: " .. xml.stag(file, "position"))
-- display e-mail addresss
print("E-mail: " .. xml.stag(file, "email"))
-- display home page
print("Home page: " .. xml.stag(file, "homepage"))



Below is my test XML file:

<employees>
<employee>
<first>Phoenix</first>
<last>Lightfoot</last>
<position>Lead Programmer</position>
<email>p.lightfoot@pathetecsys.com</email>
<homepage>http://www.pathetecsys.com/~lightfoot</homepage>
<telephone type="office">001 222 222</telephone>
<telephone type="mobile">777 222 333</telephone>
</employee>
<employee>
<first>Hugo</first>
<last>Jacob-Jones</last>
<position>Chief Executive Officer</position>
<email>h.jacobjones@pathetecsys.com</email>
<homepage>http://www.pathetecsys.com/~jacobjones</homepage>
<telephone type="office">001 222 200</telephone>
<telephone type="mobile">577 242 321</telephone>
</employee>
</employees>


Here is the console output from running my test program under the Lua interpreter:


Name: Lightfoot, Phoenix
Position: Lead Programmer
E-mail: p.lightfoot@pathetecsys.com
Home page: http://www.pathetecsys.com/~lightfoot


So far, so good. :)

I have yet to add the capability to specify an occurrence of a tag, based on its index and functions for parsing tags which contain attributes such as
<tag attribute="value">anothervalue</tag>


I should have that done soon enough.




Featured image courtesy of lambdageek (CC-BY-NC)



Digg This Story

Thursday 15 November 2007

An XML + Lua Project


Ah... XML. Nicely structured data.

There's been a few too many political posts lately. Not wanting to disappoint my (most probable) only reader, Tim, I've decided to get back on topic.


I will be using Lua to work on an upcoming project.
The project in question is a simple XML parser, which I will need later.
(Well, not need exactly. But it would be very helpful to have).



Here is a small snippet of code for the purpose of getting used to pattern matching (No, I'm not a World of Warcracker; but that is a very good reference).


function main()
local string = "tagvalue"
local match = string.gsub(string, "</?%a+>", "")
print(match) -- prints tagvalue
end
main()


Very simple stuff - here's the deal:


  1. Look in string for matches for start and end tags.

  2. Replace these tags with nothing.

  3. Print out the value contained within the pair of tags.


That will pretty much handle (with some modification of course) getting a value from within a specified pair of tags. But of course, I have to consider more advanced tags such as those with attributes.


But I will be working on this stuff shortly.




Featured image courtesy of hub19 (CC-BY-NC-SA)




Digg This Story