XmlNodeSetValue: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Fixed example)
(Do not forget xmlUnloadFile.)
 
(5 intermediate revisions by 4 users not shown)
Line 5: Line 5:
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool xmlNodeSetValue ( xmlnode theXMLNode, string value )             
bool xmlNodeSetValue ( xmlnode theXMLNode, string value [, bool setCDATA = false] )             
</syntaxhighlight>  
</syntaxhighlight>  
 
{{OOP||[[xmlnode]]:setValue|value|xmlNodeGetValue}}
===Required Arguments===  
===Required Arguments===  
*'''theXMLNode:''' The [[xml node]] you want to set the value of.
*'''theXMLNode:''' The [[xml node]] you want to set the value of.
*'''value:''' The [[string]] value you want the node to have.
*'''value:''' The [[string]] value you want the node to have.
{{New feature/item|4.0140|1.4.0|6782|
*'''setCDATA:''' A boolean indicating if you want the value to be enclosed inside CDATA tags.
}}


===Returns===
===Returns===
Line 26: Line 29:
         xmlSaveFile ( xmlFile ) -- Save the file
         xmlSaveFile ( xmlFile ) -- Save the file
     end
     end
    xmlUnloadFile(xmlFile)
end
end
</syntaxhighlight>
</syntaxhighlight>
Line 58: Line 62:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function ClientResourceStart ()
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
xmlRootTree = xmlLoadFile ( "userSettings.xml" ) --Attempt to load the xml file


Line 87: Line 90:
end
end
end
end
addEventHandler ( "onClientResourceStart", root, ClientResourceStart )
addEventHandler ( "onClientResourceStart", resourceRoot, ClientResourceStart )
   
   
function ClientResourceStop ()
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
xmlSaveFile ( xmlRootTree ) --Save the xml from memory for use next time
xmlUnloadFile ( xmlRootTree ) --Unload the xml from memory
xmlUnloadFile ( xmlRootTree ) --Unload the xml from memory
outputChatBox ( "Saved and unloaded the XML." )
outputChatBox ( "Saved and unloaded the XML." )
end
end
addEventHandler ( "onClientResourceStop", root, ClientResourceStop )
addEventHandler ( "onClientResourceStop", resourceRoot, ClientResourceStop )
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Latest revision as of 20:52, 12 May 2019

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

Syntax

bool xmlNodeSetValue ( xmlnode theXMLNode, string value [, bool setCDATA = false] )            

OOP Syntax Help! I don't understand this!

Method: xmlnode:setValue(...)
Variable: .value
Counterpart: xmlNodeGetValue


Required Arguments

  • theXMLNode: The xml node you want to set the value of.
  • value: The string value you want the node to have.
ADDED/UPDATED IN VERSION 1.4.0 r6782:
  • setCDATA: A boolean indicating if you want the value to be enclosed inside CDATA tags.

Returns

Returns true if value was successfully set, false otherwise.

Example

Click to expand [+]
Server Example
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 ()
	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", resourceRoot, ClientResourceStart )
 
function ClientResourceStop ()
	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", resourceRoot, ClientResourceStop )

See Also