XmlNodeSetAttribute: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
Line 5: Line 5:
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool xmlNodeSetAttribute ( xmlnode xmlnode, string name, var value )             
bool xmlNodeSetAttribute ( xmlnode node, string name, var value )             
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''xmlnode:''' The node in which you wish to edit the 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.


===Returns===
===Returns===
Returns ''true'' if edit was successful, ''false'' if the node and/or attribute do not exist, or if they're faulty.
Returns ''true'' if the attribute was set successfully, ''false'' if the node and/or attribute do not exist, or if they're faulty.


==Example==
==Example==
Line 31: Line 31:


function changeConfigMarkerColor(player, command, r, g, b)
function changeConfigMarkerColor(player, command, r, g, b)
     markernode = xmlFindSubNode(config, "markers", 0)
     local markernode = xmlFindSubNode(config, "markers", 0)
     xmlNodeSetAttribute(markernode, "color", r .. "," .. g .. "," .. b)
     xmlNodeSetAttribute(markernode, "color", r .. "," .. g .. "," .. b)
end
end
addCommandHandler("markercolor", changeConfigMarkerColor)
addCommandHandler("markercolor", changeConfigMarkerColor)
</syntaxhighlight>
</syntaxhighlight>


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]].
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]].

Revision as of 15:09, 21 August 2007

This function is used to edit an attribute of a node in a configuration file.

Syntax

bool xmlNodeSetAttribute ( xmlnode node, string name, var value )             

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.

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

In a gamemode, we want a command to change the marker color in the configuration file.

config.xml:

<config>
    <markers color="255,100,0" />
</config>

Lua code:

config = xmlLoadFile("config.xml")

function changeConfigMarkerColor(player, command, r, g, b)
    local markernode = xmlFindSubNode(config, "markers", 0)
    xmlNodeSetAttribute(markernode, "color", r .. "," .. g .. "," .. b)
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.

See Also

Template:FunctionArea Functions