From 0d63a3ed2459621aea1a02cbba67e103252f9e22 Mon Sep 17 00:00:00 2001 From: Antoine Cellerier Date: Sat, 13 Feb 2010 15:16:10 +0100 Subject: [PATCH] Add a simplexml lua module to parse an xml into a table. --- share/Makefile.am | 3 +- share/lua/modules/simplexml.lua | 68 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 share/lua/modules/simplexml.lua diff --git a/share/Makefile.am b/share/Makefile.am index 8de6857c30..c55124f3a6 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 0000000000..13d9a0bc83 --- /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 + + 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 -- 2.39.2