XmlNodeSetAttribute: Difference between revisions
Jump to navigation
Jump to search
(add oop syntax) |
m (Added notes about removing attributes and improved example code) |
||
Line 12: | Line 12: | ||
*'''node:''' The node of which you wish to edit an attribute. | *'''node:''' The node of which you wish to edit an attribute. | ||
*'''name:''' The name of the attribute. | *'''name:''' The name of the attribute. | ||
*'''value:''' The value which you wish to change the attribute to. | *'''value:''' The value which you wish to change the attribute to. ('''Note:''' ''nil'' will delete the attribute) | ||
===Returns=== | ===Returns=== | ||
Line 19: | Line 19: | ||
==Example== | ==Example== | ||
<section name="Server" class="server" show="true"> | <section name="Server" class="server" show="true"> | ||
In a gamemode, we want a command to change the marker color in the configuration file. | In a gamemode, we want a command to change the marker color in the configuration file and remove a deprecated attribute. | ||
config.xml: | config.xml: | ||
<syntaxhighlight lang="xml"> | <syntaxhighlight lang="xml"> | ||
<config> | <config> | ||
<markers color="255,100,0" /> | <markers color="255,100,0" foo="deprecated" /> | ||
</config> | </config> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 30: | Line 30: | ||
Lua code: | Lua code: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function changeConfigMarkerColor(thePlayer, command, r, g, b) | function changeConfigMarkerColor(thePlayer, command, r, g, b) | ||
local config = xmlLoadFile("config.xml") | |||
local markernode = xmlFindChild(config, "markers", 0) | local markernode = xmlFindChild(config, "markers", 0) | ||
xmlNodeSetAttribute(markernode, "color", r .. "," .. g .. "," .. b) | xmlNodeSetAttribute(markernode, "color", r .. "," .. g .. "," .. b) | ||
xmlNodeSetAttribute(markernode, "foo", nil) -- remove 'foo' attribute | |||
xmlSaveFile(config) | |||
xmlUnloadFile(config) | |||
end | end | ||
addCommandHandler("markercolor", changeConfigMarkerColor) | addCommandHandler("markercolor", changeConfigMarkerColor) |
Revision as of 22:39, 24 March 2016
This function is used to edit an attribute of a node in a configuration file.
Syntax
bool xmlNodeSetAttribute ( xmlnode node, string name, string/float value )
OOP Syntax Help! I don't understand this!
- Method: xmlnode:setAttribute(...)
Required Arguments
- node: The node of which you wish to edit an attribute.
- name: The name of the attribute.
- value: The value which you wish to change the attribute to. (Note: nil will delete the attribute)
Returns
Returns true if the attribute was set successfully, false if the node and/or attribute do not exist, or if they're faulty.
Example
Click to collapse [-]
ServerIn a gamemode, we want a command to change the marker color in the configuration file and remove a deprecated attribute.
config.xml:
<config> <markers color="255,100,0" foo="deprecated" /> </config>
Lua code:
function changeConfigMarkerColor(thePlayer, command, r, g, b) local config = xmlLoadFile("config.xml") local markernode = xmlFindChild(config, "markers", 0) xmlNodeSetAttribute(markernode, "color", r .. "," .. g .. "," .. b) xmlNodeSetAttribute(markernode, "foo", nil) -- remove 'foo' attribute xmlSaveFile(config) xmlUnloadFile(config) end addCommandHandler("markercolor", changeConfigMarkerColor)
Note that this example only changes the in-memory XML document. If you want to save the changed document back to the hard drive, use xmlSaveFile.