XmlNodeGetAttribute: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 20: Line 20:
settings.xml
settings.xml
<syntaxhighlight lang="xml">
<syntaxhighlight lang="xml">
<car model="528" />
<car model="528" posX="123.4" posY="456.7" posZ="12.3" rot="90.0" />
</syntaxhighlight>
</syntaxhighlight>


Lua code
Lua code
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
xml = getResourceConfig("settings.xml")      -- load XML file and get its root element
local xml = getResourceConfig("myVehicles.xml")      -- load XML file and get its root element
carmodel = xmlNodeGetAttribute(xml, "model")    -- get attribute of root element
local carmodel = xmlNodeGetAttribute(xml, "model")    -- get attribute of root element
...
local carX = xmlNodeGetAttribute(xml, "posX")
function createCar(x, y, z)
local carY = xmlNodeGetAttribute(xml, "posY")
createVehicle(carmodel, x, y, z)
local carZ = xmlNodeGetAttribute(xml, "posZ")
end
local carA = xmlNodeGetAttribute(xml, "rot")
createVehicle(carmodel, carX, carY, carZ, 0.0, 0.0, carA)
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{XML_functions}}
{{XML_functions}}

Revision as of 20:59, 24 March 2009

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

Syntax

string xmlNodeGetAttribute ( xmlnode node, string name )             

Required Arguments

  • node: The node from which you wish to return the attribute
  • name: The name of the attribute.

Returns

Returns the attribute in string form.

Example

Suppose we have a gametype where only one type of car is used, and this type should not depend on the map but rather be set in an external configuration file and be used in all maps. Here's an example where the configuration file is an XML document:

settings.xml

<car model="528" posX="123.4" posY="456.7" posZ="12.3" rot="90.0" />

Lua code

local xml = getResourceConfig("myVehicles.xml")      -- load XML file and get its root element
local carmodel = xmlNodeGetAttribute(xml, "model")    -- get attribute of root element
local carX = xmlNodeGetAttribute(xml, "posX")
local carY = xmlNodeGetAttribute(xml, "posY")
local carZ = xmlNodeGetAttribute(xml, "posZ")
local carA = xmlNodeGetAttribute(xml, "rot")
createVehicle(carmodel, carX, carY, carZ, 0.0, 0.0, carA)

See Also