[vlc-devel] commit: Add a simplexml lua module to parse an xml into a table. ( Antoine Cellerier )

git version control git at videolan.org
Sat Feb 13 16:39:05 CET 2010


vlc | branch: master | Antoine Cellerier <dionoea at videolan.org> | Sat Feb 13 15:16:10 2010 +0100| [0d63a3ed2459621aea1a02cbba67e103252f9e22] | committer: Antoine Cellerier 

Add a simplexml lua module to parse an xml into a table.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=0d63a3ed2459621aea1a02cbba67e103252f9e22
---

 share/Makefile.am               |    3 +-
 share/lua/modules/simplexml.lua |   68 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+), 1 deletions(-)

diff --git a/share/Makefile.am b/share/Makefile.am
index 8de6857..c55124f 100644
--- a/share/Makefile.am
+++ b/share/Makefile.am
@@ -220,7 +220,8 @@ DIST_lua= \
 	lua/intf/modules/host.lua \
 	lua/intf/telnet.lua \
 	lua/intf/dummy.lua \
-	lua/modules/sandbox.lua
+	lua/modules/sandbox.lua \
+	lua/modules/simplexml.lua
 
 DIST_http_lua = \
 	lua/http/.hosts \
diff --git a/share/lua/modules/simplexml.lua b/share/lua/modules/simplexml.lua
new file mode 100644
index 0000000..13d9a0b
--- /dev/null
+++ b/share/lua/modules/simplexml.lua
@@ -0,0 +1,68 @@
+--[==========================================================================[
+ simplexml.lua: Lua simple xml parser wrapper
+--[==========================================================================[
+ Copyright (C) 2010 Antoine Cellerier
+ $Id$
+
+ Authors: Antoine Cellerier <dionoea at videolan dot org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+--]==========================================================================]
+
+module("simplexml",package.seeall)
+
+--[[ Returns the xml tree structure
+--   Each node is of one of the following types:
+--     { name (string), attributes (key->value map), children (node array) }
+--     text content (string)
+--]]
+
+local function parsexml(stream)
+    local xml = vlc.xml()
+    local reader = xml:create_reader(stream)
+
+    local tree
+    local parents = {}
+    while reader:read() > 0 do
+        local nodetype = reader:node_type()
+        if nodetype == 'startelem' then
+            local name = reader:name()
+            local node = { name: '', attributes: {}, children: {} }
+            node.name = name
+            while reader:NextAttr() == 0 do
+                node.attributes[reader:Name()] = reader:Value()
+            end
+            if tree then
+                tree.children[#tree.children] = node
+                parents[#parents] = tree
+                tree = node
+            end
+        elseif nodetype == 'endelem' then
+            tree = parents[#parents-1]
+        elseif nodetype == 'text' then
+            node.children[#node.children] = reader:Value()
+        end
+    end
+
+    return tree
+end
+
+function parse_url(url)
+    return parsexml(vlc.stream(url))
+end
+
+function parse_string(str)
+    return parsexml(vlc.memory_stream(str))
+end




More information about the vlc-devel mailing list