XmlFindChild: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(→‎Example: Removed references to very very old functions that don't exist, changed to be generic rather than referencing an equally old map format.)
(Do not forget xmlUnloadFile.)
 
(6 intermediate revisions by 4 users not shown)
Line 5: Line 5:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">xmlnode xmlFindChild ( xmlnode parent, string tagName, int index )</syntaxhighlight>
<syntaxhighlight lang="lua">xmlnode xmlFindChild ( xmlnode parent, string tagName, int index )</syntaxhighlight>
 
{{OOP||[[xmlnode]]:findChild}}
===Required Arguments===
===Required Arguments===
* '''parent''': This is an [[xmlnode]] that you want to find the child node under.
* '''parent''': This is an [[xmlnode]] that you want to find the child node under.
* '''tagName''': This is the name of the child node you wish to find.
* '''tagName''': This is the name of the child node you wish to find (case-sensitive).
* '''index''': This is the 0-based index of the node you wish to find. For example, to find the 5th subnode with a particular name, you would use 4 as the index value. To find the first occurence, use 0.
* '''index''': This is the 0-based index of the node you wish to find. For example, to find the 5th subnode with a particular name, you would use 4 as the index value. To find the first occurence, use 0.


Line 16: Line 16:
==Example==
==Example==
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
If you wanted to find an 'instructions' node in an xml file like this:
If you wanted to find an ''instructions'' node in an xml file like this:
<syntaxhighlight lang="xml">
<syntaxhighlight lang="xml">
<root version="2.0">
<root version="2.0">
Line 25: Line 25:
</syntaxhighlight>
</syntaxhighlight>


You could use the following code:
You could use the following code to print the text in the ''instructions'' node to the chatbox:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
root = xmlLoadFile("test.xml")
local rootNode = xmlLoadFile ( "test.xml" )
optionsnode = xmlFindChild ( root, "options", 0 )
local optionsNode = xmlFindChild ( rootNode, "options", 0 )
instructionsnode = xmlFindChild ( optionsnode, "instructions", 0 )
local instructionsNode = xmlFindChild ( optionsNode, "instructions", 0 )
local instructions = xmlNodeGetValue ( instructionsNode )
xmlUnloadFile(rootNode)
 
outputChatBox ( instructions )
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
Line 35: Line 39:
==See Also==
==See Also==
{{XML functions}}
{{XML functions}}
[[ru:xmlFindChild]]

Latest revision as of 20:47, 12 May 2019

This function returns a named child node of an XML node.

Syntax

xmlnode xmlFindChild ( xmlnode parent, string tagName, int index )

OOP Syntax Help! I don't understand this!

Method: xmlnode:findChild(...)


Required Arguments

  • parent: This is an xmlnode that you want to find the child node under.
  • tagName: This is the name of the child node you wish to find (case-sensitive).
  • index: This is the 0-based index of the node you wish to find. For example, to find the 5th subnode with a particular name, you would use 4 as the index value. To find the first occurence, use 0.

Returns

Returns an xmlnode if the node was found, false otherwise.

Example

Click to collapse [-]
Server

If you wanted to find an instructions node in an xml file like this:

<root version="2.0">
      <options>
            <instructions>Start at the beginning and keep going until the end!</instructions>
      </options>
</root>

You could use the following code to print the text in the instructions node to the chatbox:

local rootNode = xmlLoadFile ( "test.xml" )
local optionsNode = xmlFindChild ( rootNode, "options", 0 )
local instructionsNode = xmlFindChild ( optionsNode, "instructions", 0 )
local instructions = xmlNodeGetValue ( instructionsNode )
xmlUnloadFile(rootNode)

outputChatBox ( instructions )

See Also