Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ Write to an xml file.
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Met_fredrik
(4 posts) Bio
|
Date
| Sat 11 Nov 2006 11:46 PM (UTC) |
Message
| Hey, as the title says I am going to write to an xml file(edit) with lua. If I got something like this code in a file called summon.xml,
<summons>
<summon name="Dog" mana="220"/>
<summon name="Rabbit" mana="220"/>
<summon name="Deer" mana="260"/>
<summon name="Pig" mana="255"/>
</summons>
How can I add a new line before the last line </summons> in the easiest possible way?
Thanks in advance. | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #1 on Sun 12 Nov 2006 04:16 AM (UTC) |
Message
| The easiest possible way is probably to simply read (and make a copy of) the file until you reach the line with "</summons>" on it, and write the new line before it.
A more sophisticated way would be to actually parse the XML into a table and then write it out again, but I suppose that is not the "easiest possible" way.
MUSHclient has an XML parser built in, see the function utils.xmlread on the page:
http://www.gammon.com.au/scripts/doc.php?general=lua
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Met_fredrik
(4 posts) Bio
|
Date
| Reply #2 on Sun 12 Nov 2006 11:58 AM (UTC) Amended on Sun 12 Nov 2006 12:32 PM (UTC) by Met_fredrik
|
Message
| Thanks.
I need the parsing it self to be done with pure lua, not within the MUSHclient. Got any code examples for that?
Lua is able to parse simple xml into tables without addons right? | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #3 on Sun 12 Nov 2006 07:37 PM (UTC) |
Message
| Not exactly. There is a reference to an XML parser in the Programming In Lua book:
http://www.lua.org/pil/29.2.html
However I think that relies upon an external parser written in C.
In your case, you seem to be trying to keep data related to a MUD in a file. Why not just use straight Lua tables, serialized to disk?
It could look like this:
summons = {}
summons.Dog = 220
summons.Rabbit = 220
summons.Deer = 260
summons.Pig = 255
require "serialize"
a = serialize.save ("summons"))
print (a)
This will print out the above table. You could instead write it to disk (that is, write variable "a" to a disk file).
Then you load it back in like this:
This is a lot simpler than parsing XML, and is one of the things Lua was designed to do.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #4 on Sun 12 Nov 2006 07:40 PM (UTC) |
Message
| If you want to store more stuff than just mana, just make subtables, like this:
summons = {}
summons.Dog = {}
summons.Dog.mana = 220
summons.Dog.hp = 40
summons.Rabbit = {}
summons.Rabbit.mana = 220
summons.Rabbit.hp = 50
summons.Deer = {}
summons.Deer.mana = 260
summons.Deer.hp = 70
summons.Pig = {}
summons.Pig.mana = 255
summons.Pig.hp = 80
The loading and saving would be the same. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Met_fredrik
(4 posts) Bio
|
Date
| Reply #5 on Sun 12 Nov 2006 08:14 PM (UTC) Amended on Sun 12 Nov 2006 08:19 PM (UTC) by Met_fredrik
|
Message
| The data is allready stored in xml files, I just want the possibility of changing the xml files using lua. Etc, using an item ingame wich changes the xml file that is controlling what summons the player can have.
If you understand what I mean:)
Btw, is the xml parser in c you linked for, an external program, like I have to have it in the background? Or do I have to implement it into the game server?
Thankyou very much for answers so far:) | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #6 on Tue 14 Nov 2006 07:31 PM (UTC) |
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 |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #7 on Tue 14 Nov 2006 07:47 PM (UTC) |
Message
| This page has lots of information on handling XML from Lua. In particular it includes a Lua-only parser. However, I'm not sure if that code is 5.1, 5.0, or from before; I didn't really look at it beyond seeing it was a Lua-only parser.
http://lua-users.org/wiki/LuaXml |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #8 on Tue 14 Nov 2006 07:54 PM (UTC) |
Message
| Looks like Roberto's version would be much more sophisticated than mine, and is reasonably short, too.
I was giving an example of how you can use Lua to quickly parse a given text file (in this case, simple XML). That link is a great source of more complex parsers, which would apply to "real-world" XML.
I little while back I did a variant on the simple code above to parse the SMAUG help files (and commands file) as they have a simple format, and this sort of parser is perfectly adequate in those cases. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #9 on Tue 14 Nov 2006 07:59 PM (UTC) Amended on Tue 14 Nov 2006 08:02 PM (UTC) by Nick Gammon
|
Message
| By the way, I discovered a new feature hidden away in Lua 5.1, namely that you can use string.gsub to do a replacement using a table. Here is an example of implementing the HTML fixup (< becomes < and so on) using a simple function:
function fixhtml (s)
return (string.gsub (tostring (s), "[<>&]",
{ ["<"] = "<", [">"] = ">", ["&"] = "&" }
))
end -- fixhtml
-- test
print (fixhtml ("<dog> & <cat>")) --> <dog> & <cat>
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Met_fredrik
(4 posts) Bio
|
Date
| Reply #10 on Thu 16 Nov 2006 04:18 PM (UTC) |
Message
| So much answers! I am overwhelmed! I will get on to it right away! Thanks for all answers:) | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
26,243 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top