| Message |
It was an external program, I think, I haven't used it. It would need to be a DLL or shared library.
However if your file consists of the exact text you showed there (or similar) then you could make a cut-down XML parser in straight Lua. Here is an example:
xmltext = [[
<summons>
<summon name="Dog" mana="220"/>
<summon name="Rabbit" mana="220"/>
<summon name="Deer" mana="260"/>
<summon name="Pig" mana="255"/>
</summons>
]]
xmltable = {}
function analyse_tag (name, params)
local item = { _name = name }
table.insert (xmltable, item)
-- process invididual elements
string.gsub (params, '(%a+)%s*=%s*"(.-)"',
function (n, v)
item [n] = v
end -- function
)
end -- analyse_tag
-- process all text
string.gsub (xmltext, "<(%a+) (.-)/>", analyse_tag)
require "tprint"
tprint (xmltable)
This code analyzes some XML text (you would write your own code to read the file, which is a couple of lines), and when run it puts it into a table. The "tprint" function in the last couple of lines is just for debugging to show what it has done, namely:
1:
"_name"="summon"
"name"="Dog"
"mana"="220"
2:
"_name"="summon"
"name"="Rabbit"
"mana"="220"
3:
"_name"="summon"
"name"="Deer"
"mana"="260"
4:
"_name"="summon"
"name"="Pig"
"mana"="255"
Now you could add to that table, and write it back as XML easily enough, like this:
for _, v in ipairs (xmltable) do
line = "<" .. v._name
for tag, contents in pairs (v) do
if tag ~= "_name" then
line = line .. " " .. tag .. '="' .. contents .. '"'
end -- not _name
end -- for
line = line .. "/>"
print (line)
end -- for loop
Running this on the above table gives:
<summon name="Dog" mana="220"/>
<summon name="Rabbit" mana="220"/>
<summon name="Deer" mana="260"/>
<summon name="Pig" mana="255"/>
Now all you have to do is put <summons> and </summons> around it.
This XML parser is fairly simple, for example it doesn't handle things like &, < or > inside the text, although that would be easy enough to do. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|