XmlNodeGetChildren: Difference between revisions
Jump to navigation
Jump to search
(New page: __NOTOC__ {{Server client function}} This function returns a single sub node or a list of sub nodes of a particular XML node. ==Syntax== <syntaxhighlight lang="lua">xmlnode/table xmlNodeGetSubNodes( xmlno...) |
No edit summary |
||
(16 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server client function}} | {{Server client function}} | ||
This function returns a | |||
This function returns all children of a particular XML node, or a particular child node. | |||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua">xmlnode | <syntaxhighlight lang="lua">table/xmlnode xmlNodeGetChildren ( xmlnode parent, [ int index ] )</syntaxhighlight> | ||
{{OOP||[[xmlnode]]:getChildren|children}} | |||
===Required Arguments=== | ===Required Arguments=== | ||
* '''parent''' | * '''parent:''' This is the [[xmlnode]] you want to retrieve one or all child nodes of. | ||
===Optional Arguments=== | ===Optional Arguments=== | ||
* '''index:''' If you only want to retrieve one particular child node, specify its (0-based) index here. For example if you only want the first node, specify 0; the fifth node has index 4, etc. | |||
* '''index''' | |||
===Returns=== | ===Returns=== | ||
If index isn't specified, | If '''index''' isn't specified, returns a table containing all child nodes. If '''index''' is specified, returns the corresponding child node if it exists. If no nodes are found, it returns an empty table. Returns ''false'' in case of failure. | ||
==Example== | ==Example== | ||
<section name="Server" class="server" show="true"> | <section name="Server" class="server" show="true"> | ||
Suppose you have an .xml file with random welcome messages: | |||
<syntaxhighlight lang="xml"> | <syntaxhighlight lang="xml"> | ||
< | <messages> | ||
<message>Welcome to the deathmatch server, enjoy your stay.</message> | |||
<message>Welcome. Be sure to get your free pizza at Matt's!</message> | |||
<message>Party going on at the LS beach, be there</message> | |||
</ | </messages> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
To show a random message from this list to joining players, you could use the following code: | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
addEventHandler("onResourceStart", getResourceRootElement(), | |||
function() | |||
local xml = xmlLoadFile("welcome.xml") -- open the XML file | |||
local messageNodes = xmlNodeGetChildren(xml) -- get all child nodes of the root node (<messages>) | |||
g_WelcomeMessages = {} -- create a new global variable to store the welcome messages | |||
for i,node in ipairs(messageNodes) do -- loop over all the message nodes | |||
g_WelcomeMessages[i] = xmlNodeGetValue(node) -- retrieve the text in each node | |||
end | |||
xmlUnloadFile(xml) -- close the XML file | |||
end | |||
) | |||
addEventHandler("onPlayerJoin", getRootElement(), | |||
function() | |||
local numMessages = #g_WelcomeMessages -- get the number of messages | |||
local message = g_WelcomeMessages[math.random(numMessages)] -- pick a random message | |||
outputChatBox(message, source, 0, 255, 0) -- display it to the joining player | |||
end | |||
) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | </section> | ||
==See Also== | ==See Also== | ||
{{XML functions}} | {{XML functions}} | ||
[[ru:xmlNodeGetChildren]] |
Latest revision as of 20:29, 23 September 2016
This function returns all children of a particular XML node, or a particular child node.
Syntax
table/xmlnode xmlNodeGetChildren ( xmlnode parent, [ int index ] )
OOP Syntax Help! I don't understand this!
- Method: xmlnode:getChildren(...)
- Variable: .children
Required Arguments
- parent: This is the xmlnode you want to retrieve one or all child nodes of.
Optional Arguments
- index: If you only want to retrieve one particular child node, specify its (0-based) index here. For example if you only want the first node, specify 0; the fifth node has index 4, etc.
Returns
If index isn't specified, returns a table containing all child nodes. If index is specified, returns the corresponding child node if it exists. If no nodes are found, it returns an empty table. Returns false in case of failure.
Example
Click to collapse [-]
ServerSuppose you have an .xml file with random welcome messages:
<messages> <message>Welcome to the deathmatch server, enjoy your stay.</message> <message>Welcome. Be sure to get your free pizza at Matt's!</message> <message>Party going on at the LS beach, be there</message> </messages>
To show a random message from this list to joining players, you could use the following code:
addEventHandler("onResourceStart", getResourceRootElement(), function() local xml = xmlLoadFile("welcome.xml") -- open the XML file local messageNodes = xmlNodeGetChildren(xml) -- get all child nodes of the root node (<messages>) g_WelcomeMessages = {} -- create a new global variable to store the welcome messages for i,node in ipairs(messageNodes) do -- loop over all the message nodes g_WelcomeMessages[i] = xmlNodeGetValue(node) -- retrieve the text in each node end xmlUnloadFile(xml) -- close the XML file end ) addEventHandler("onPlayerJoin", getRootElement(), function() local numMessages = #g_WelcomeMessages -- get the number of messages local message = g_WelcomeMessages[math.random(numMessages)] -- pick a random message outputChatBox(message, source, 0, 255, 0) -- display it to the joining player end )