[Feature Request] lib/mods/xml.lua - Write to XML files.

Discuss, ask for help, share ideas, give suggestions, read tutorials, and tell us about bugs you have found with MicroMacro in here.

Do not post RoM-Bot stuff here. There is a subforum for that.
Forum rules
This is a sub-forum for things specific to MicroMacro.

This is not the place to ask questions about the RoM bot, which uses MicroMacro. There is a difference.
Post Reply
Message
Author
zer0
Posts: 213
Joined: Sat Feb 16, 2008 11:55 pm

[Feature Request] lib/mods/xml.lua - Write to XML files.

#1 Post by zer0 » Thu Mar 26, 2009 5:53 am

Elv I have found that your XML library module is invaluable. Nearly all my configuration data now exists in XML files.

There is one thing that I would like it to do though. To easily be able to write to the files as well with set (mutator) functions.

Reason I would like this is I think I have figured out how the findPatternInProcess function works from reading your posts on 1.0, and I would like to have the memory offsets once located be saved in a XML config file.
I know that you can save them in a lua file easily like you have done with the ROM bot.

But I do see a need for being able to write to the XML files as well.

Or is it relatively simple to write XML files with the LuaExpat library and no wrapper functions need to be created?

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: [Feature Request] lib/mods/xml.lua - Write to XML files.

#2 Post by Administrator » Thu Mar 26, 2009 2:55 pm

It's something I've thought about before, but there are a few problems with it. First and foremost is with the parser. In some instances, it strips certain characters or groups of characters. In some cases, it would seem to be a feature, in others a bug. It's hard to say.

Code: Select all

<someElement>
    this is the value.
</someElement>
In this example, the someElement's value would have it's preceding and trailing newline characters stripped. This would seem to be a feature, and just a minor annoyance when you want to keep them.

Code: Select all

<someElement>
    --[[ This is a comment ]]

    index = 0;
</someElement>
In this example, everything gets screwed up. This has serious problems with placing Lua code inside XML tags. For whatever reason, it sometimes decides to strip the ']]' from lines, and other times it leaves them. It's a guess and check kind of game.

Plus, there's also a problem with casting now. Values supplied as hex are converted to decimal on parse, which means they will be saved as decimal.


That being said, here's a work-in-progress:

Code: Select all

-- Write a node to a file
function Node:write(filename)
	local outfile, err = io.open(filename, "w");
	if( outfile == nil ) then
		error(err, 2);
	end

	local level = 0;

	local function write_subnodes(node)
		--node:debug();

		if( level > 0 ) then
			outfile:write("\n");
		end

		outfile:write(string.rep("\t", level) .. "<" .. node._NAME);
		local i = 1;
		while(true) do
			local name = node._ATTRIBUTES[i];
			if( name == nil ) then
				break;
			end;

			local value = node:getAttribute(name);
			i = i + 1;

			outfile:write(" " .. name .. "=\"" .. value .. "\"");
		end

		local elements = node:getElements();

		if( node._VALUE or #elements > 0 ) then
			outfile:write(">");
		else
			outfile:write(" />");
		end


		if( node._VALUE ) then
			outfile:write(node._VALUE);
		end

		level = level + 1;
		for i,v in pairs(elements) do
			write_subnodes(v);
		end
		level = level - 1;

		if( node._VALUE or #elements > 0 ) then
			if( #elements > 1 or (node._VALUE and string.find(node._VALUE, "\n")) ) then
				outfile:write("\n" .. string.rep("\t", level) .. "</" .. node._NAME .. ">");
			else
				outfile:write("</" .. node._NAME .. ">");
			end

			if( level > 0 ) then
				outfile:write("\n");
			end
		end
	end

	write_subnodes(self);

end
Just call write on your root node, and it should work.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests