Skip to content

Instantly share code, notes, and snippets.

Created December 24, 2016 19:07
Show Gist options
  • Save anonymous/5d99931170b50556a92b5ef287f2cbae to your computer and use it in GitHub Desktop.
Save anonymous/5d99931170b50556a92b5ef287f2cbae to your computer and use it in GitHub Desktop.
Makinit's XML library (reduced version)
-- Makinit's xml library
do
local namePattern = "[%a_:][%w%.%-_:]*"
function parseXml(xml)
local root = {}
local parents = {}
local element = root
for closing, name, attributes, empty, text in string.gmatch(xml, "<(/?)(" .. namePattern .. ")(.-)(/?)>%s*([^<]*)%s*") do
if closing == "/" then
local parent = parents[element]
if parent and name == element.name then
element = parent
end
else
local child = {name = name, attribute = {}}
table.insert(element, child)
parents[child] = element
if empty ~= "/" then
element = child
end
for name, value in string.gmatch(attributes, "(" .. namePattern .. ")%s*=%s*\"(.-)\"") do
child.attribute[name] = value
end
end
if text ~= "" then
local child = {text = text}
table.insert(element, child)
parents[child] = element
end
end
return root[1]
end
function path(nodes, ...)
nodes = {nodes}
for i, name in ipairs(arg) do
local match = {}
for i, node in ipairs(nodes) do
for i, child in ipairs(node) do
if child.name == name then
table.insert(match, child)
end
end
end
nodes = match
end
return nodes
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment