XmlNodeGetValue: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Do not forget xmlUnloadFile.)
 
(11 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:Incomplete]]
{{Server client function}}
 
__NOTOC__  
__NOTOC__  
This function is made to be able to read values in XML files. (for example <something>anything</something>)  
This function is made to be able to read tag values in XML files (eg. <something>anything</something>).


==Syntax==  
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string xmlNodeGetValue ( xmlnode xmlnode )             
string xmlNodeGetValue ( xmlnode theXMLNode )             
</syntaxhighlight>  
</syntaxhighlight>  
{{OOP||[[xmlnode]]:getValue|value|xmlNodeSetValue}}


===Required Arguments===  
===Required Arguments===  
*'''xmlnode:''' The node of which you need to know the value.
*'''theXMLNode:''' The [[xml node]] of which you need to know the value.
 
===Optional Arguments===
{{OptionalArg}}
*'''none:'''


===Returns===
===Returns===
Returns the value of the node if successful, ''false'' otherwise.
Returns the value of the node as a [[string]] if it was received successfully, ''false'' otherwise.


==Example==  
==Example==  
In this example is shown what xmlNodeSetValue does and how it works:
<section name="Server Example: return a sample file from a XML file" class="server" show="true">
In this example a sample value is returned from a XML file.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local xmlFile=xmlLoadFile("xmlfile.xml") --Open a file already created
local xmlFile = xmlLoadFile ( "exampleFile.xml" ) -- Open a file that we have already created
if xmlFile then --If it's indeed opened:
if xmlFile then -- If it's indeed opened
        local node=xmlCreateSubNode(xmlFile,"somesubnode") --Create a new subnode
    local node = xmlFindChild( xmlFile, "somesubnode", 0 ) -- Find our first sub node
        local success=xmlNodeSetValue(node,"somevalue") --Set the value of it
    local success = xmlNodeGetValue ( node ) -- Get the value of it
                if success then --Check if it was successful
    xmlUnloadFile(xmlFile)
                        xmlSaveFile(xmlFile) --Save the file
    if success then -- Check if it was successful
                end --End what still needs to be ended
        outputChatBox ( tostring ( success ) ) -- Output "somevalue" to the chatbox
        end
    end
end
end
</syntaxhighlight>
</syntaxhighlight>
The xml file wil look like:  
In order for the result to be valid, the xml file has to look like this:
<section name="exampleFile.xml" class="server" show="true">
<syntaxhighlight lang="xml">
<syntaxhighlight lang="xml">
<somenode>
<somenode>
Line 38: Line 36:
</somenode>
</somenode>
</syntaxhighlight>
</syntaxhighlight>
</section>
<section name="Client Example: Save and load from a clientside XML" class="client" show="true">
This shows an example of a clientside XML file. You can use this to store user preferences and load them the next time the script loads. Almost always, you should have an options GUI for clients to interact with to set these values.
<br/><br/>
Since the XML will change, it should NOT be included in the resource's meta.xml file. MTA will think that file is corrupted and will download it again from the server. Instead, you should create the XML within the script, and then load it within the script on future script runs if it exists.
'''This xml will be created from the script following it below'''
<syntaxhighlight lang="xml">
<root>
    <hud_display>
        <IconSizeX>60</IconSizeX>
        <IconSizeY>60</IconSizeY>
    </hud_display>
    <hud_binds>
        <weaponSlot0>tab</weaponSlot0>
        <weaponSlot1>1</weaponSlot1>
    </hud_binds>
</root>
</syntaxhighlight>
'''This script will create the xml or load it if it exists'''
<syntaxhighlight lang="lua">
function ClientResourceStart ()
if source ~= getResourceRootElement() then return end --This event will happen with any resource start, isolate it to this resource
xmlRootTree = xmlLoadFile ( "userSettings.xml" ) --Attempt to load the xml file
if xmlRootTree then -- If the xml loaded then...
xmlHudBranch = xmlFindChild(xmlRootTree,"hud_display",0) -- Find the hud sub-node
xmlBindsBranch = xmlFindChild(xmlRootTree,"hud_binds",0) -- Find the binds sub-node
outputChatBox ( "XML Found and Loaded" )
else -- If the xml does not exist then...
xmlRootTree = xmlCreateFile ( "userSettings.xml", "root" ) -- Create the xml file
xmlHudBranch = xmlCreateChild ( xmlRootTree, "hud_display" ) -- Create the hud sub-node under the root node
xmlBindsBranch = xmlCreateChild ( xmlRootTree, "hud_binds" )-- Create the binds sub-node under the root node
xmlNodeSetValue (xmlCreateChild ( xmlHudBranch, "IconSizeX"), "60" ) --Create sub-node values under the hud sub-node
xmlNodeSetValue (xmlCreateChild ( xmlHudBranch, "IconSizeY"), "60" ) --Create sub-node values under the hud sub-node
xmlNodeSetValue (xmlCreateChild ( xmlBindsBranch, "weaponSlot0"), "tab" ) --Create sub-node values under the binds sub-node
xmlNodeSetValue (xmlCreateChild ( xmlBindsBranch, "weaponSlot1"), "1" ) --Create sub-node values under the binds sub-node
outputChatBox ( "XML Created" )
end
--Retrieve a single sub-node name or value
outputChatBox( "Node name: "..xmlNodeGetName (xmlFindChild(xmlHudBranch,"IconSizeX",0)), 0, 0, 255 ) --blue outputs
outputChatBox( "Node Value: "..xmlNodeGetValue (xmlFindChild(xmlHudBranch,"IconSizeX",0)), 0, 0, 255 ) --blue outputs
        --Retrieve multiple sub-node names or values
xmlHudChildrenTable = xmlNodeGetChildren ( xmlHudBranch ) --Create a table of this branch's children
for i,node in pairs(xmlHudChildrenTable ) do --Loop through the branch's children for sub-nodes
            outputChatBox( "Node name: "..xmlNodeGetName (node), 0, 255, 0 ) --green outputs
    outputChatBox( "Node Value: "..xmlNodeGetValue(node), 0, 255, 0 ) --green outputs
end
end
addEventHandler ( "onClientResourceStart", root, ClientResourceStart )
function ClientResourceStop ()
if source ~= getResourceRootElement() then return end --This event will happen with any resource stop, isolate it to this resource
xmlSaveFile ( xmlRootTree ) --Save the xml from memory for use next time
xmlUnloadFile ( xmlRootTree ) --Unload the xml from memory
outputChatBox ( "Saved and unloaded the XML." )
end
addEventHandler ( "onClientResourceStop", root, ClientResourceStop )
</syntaxhighlight>
</section>
</section>


==See Also==
==See Also==
{{FunctionArea_Functions}}
{{XML functions}}
{{XML_functions}}

Latest revision as of 20:50, 12 May 2019

This function is made to be able to read tag values in XML files (eg. <something>anything</something>).

Syntax

string xmlNodeGetValue ( xmlnode theXMLNode )             

OOP Syntax Help! I don't understand this!

Method: xmlnode:getValue(...)
Variable: .value
Counterpart: xmlNodeSetValue


Required Arguments

  • theXMLNode: The xml node of which you need to know the value.

Returns

Returns the value of the node as a string if it was received successfully, false otherwise.

Example

Click to collapse [-]
Server Example: return a sample file from a XML file

In this example a sample value is returned from a XML file.

local xmlFile = xmlLoadFile ( "exampleFile.xml" ) -- Open a file that we have already created
if xmlFile then -- If it's indeed opened
    local node = xmlFindChild( xmlFile, "somesubnode", 0 ) -- Find our first sub node
    local success = xmlNodeGetValue ( node ) -- Get the value of it
    xmlUnloadFile(xmlFile)
    if success then -- Check if it was successful
        outputChatBox ( tostring ( success ) ) -- Output "somevalue" to the chatbox
    end
end

In order for the result to be valid, the xml file has to look like this: <section name="exampleFile.xml" class="server" show="true">

<somenode>
        <somesubnode>somevalue</somesubnode>
</somenode>
Click to collapse [-]
Client Example: Save and load from a clientside XML

This shows an example of a clientside XML file. You can use this to store user preferences and load them the next time the script loads. Almost always, you should have an options GUI for clients to interact with to set these values.

Since the XML will change, it should NOT be included in the resource's meta.xml file. MTA will think that file is corrupted and will download it again from the server. Instead, you should create the XML within the script, and then load it within the script on future script runs if it exists.

This xml will be created from the script following it below

<root>
    <hud_display>
        <IconSizeX>60</IconSizeX>
        <IconSizeY>60</IconSizeY>
    </hud_display>
    <hud_binds>
        <weaponSlot0>tab</weaponSlot0>
        <weaponSlot1>1</weaponSlot1>
    </hud_binds>
</root>

This script will create the xml or load it if it exists

function ClientResourceStart ()
	if source ~= getResourceRootElement() then return end --This event will happen with any resource start, isolate it to this resource	
	xmlRootTree = xmlLoadFile ( "userSettings.xml" ) --Attempt to load the xml file	

	if xmlRootTree then -- If the xml loaded then...
		xmlHudBranch = xmlFindChild(xmlRootTree,"hud_display",0) -- Find the hud sub-node
		xmlBindsBranch = xmlFindChild(xmlRootTree,"hud_binds",0) -- Find the binds sub-node
		outputChatBox ( "XML Found and Loaded" )
	else -- If the xml does not exist then...
		xmlRootTree = xmlCreateFile ( "userSettings.xml", "root" ) -- Create the xml file	
		xmlHudBranch = xmlCreateChild ( xmlRootTree, "hud_display" ) -- Create the hud sub-node under the root node
		xmlBindsBranch = xmlCreateChild ( xmlRootTree, "hud_binds" )-- Create the binds sub-node under the root node
		xmlNodeSetValue (xmlCreateChild ( xmlHudBranch, "IconSizeX"), "60" ) --Create sub-node values under the hud sub-node
		xmlNodeSetValue (xmlCreateChild ( xmlHudBranch, "IconSizeY"), "60" ) --Create sub-node values under the hud sub-node
		xmlNodeSetValue (xmlCreateChild ( xmlBindsBranch, "weaponSlot0"), "tab" ) --Create sub-node values under the binds sub-node
		xmlNodeSetValue (xmlCreateChild ( xmlBindsBranch, "weaponSlot1"), "1" ) --Create sub-node values under the binds sub-node
		outputChatBox ( "XML Created" )
	end

	--Retrieve a single sub-node name or value
	outputChatBox( "Node name: "..xmlNodeGetName (xmlFindChild(xmlHudBranch,"IconSizeX",0)), 0, 0, 255 ) --blue outputs
	outputChatBox( "Node Value: "..xmlNodeGetValue (xmlFindChild(xmlHudBranch,"IconSizeX",0)), 0, 0, 255 ) --blue outputs
	
        --Retrieve multiple sub-node names or values	
	xmlHudChildrenTable = xmlNodeGetChildren ( xmlHudBranch ) --Create a table of this branch's children
	for i,node in pairs(xmlHudChildrenTable ) do --Loop through the branch's children for sub-nodes
            outputChatBox( "Node name: "..xmlNodeGetName (node), 0, 255, 0 ) --green outputs
	    outputChatBox( "Node Value: "..xmlNodeGetValue(node), 0, 255, 0 ) --green outputs
	end
end
addEventHandler ( "onClientResourceStart", root, ClientResourceStart )
 
function ClientResourceStop ()
	if source ~= getResourceRootElement() then return end --This event will happen with any resource stop, isolate it to this resource
	xmlSaveFile ( xmlRootTree ) --Save the xml from memory for use next time
	xmlUnloadFile ( xmlRootTree ) --Unload the xml from memory
	outputChatBox ( "Saved and unloaded the XML." )
end
addEventHandler ( "onClientResourceStop", root, ClientResourceStop )

</section>

See Also