1 --[==========================================================================[
2 simplexml.lua: Lua simple xml parser wrapper
3 --[==========================================================================[
4 Copyright (C) 2010 Antoine Cellerier
7 Authors: Antoine Cellerier <dionoea at videolan dot org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 --]==========================================================================]
24 module("simplexml",package.seeall)
26 --[[ Returns the xml tree structure
27 -- Each node is of one of the following types:
28 -- { name (string), attributes (key->value map), children (node array) }
29 -- text content (string)
32 local function parsexml(stream, errormsg)
33 if not stream then return nil, errormsg end
35 local reader = xml:create_reader(stream)
39 local nodetype, nodename = reader:next_node()
43 local node = { name= nodename, attributes= {}, children= {} }
44 local attr, value = reader:next_attr()
46 node.attributes[attr] = value
47 attr, value = reader:next_attr()
50 table.insert(tree.children, node)
51 table.insert(parents, tree)
54 elseif nodetype == 2 then
57 while nodename ~= tree.name do
59 error("XML parser error/faulty logic")
62 tree = parents[#parents]
64 table.remove(tree.children)
65 table.insert(tmp, 1, child)
66 for i, node in pairs(child.children) do
67 table.insert(tmp, i+1, node)
71 for _, node in pairs(tmp) do
72 table.insert(tree.children, node)
74 tree = parents[#parents]
77 elseif nodetype == 3 then
78 table.insert(tree.children, nodename)
80 nodetype, nodename = reader:next_node()
84 error("XML parser error/Missing closing tags")
89 function parse_url(url)
90 return parsexml(vlc.stream(url))
93 function parse_string(str)
94 return parsexml(vlc.memory_stream(str))
97 function add_name_maps(tree)
98 tree.children_map = {}
99 for _, node in pairs(tree.children) do
100 if type(node) == "table" then
101 if not tree.children_map[node.name] then
102 tree.children_map[node.name] = {}
104 table.insert(tree.children_map[node.name], node)