XmlCreateFile: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(fixed example)
 
(30 intermediate revisions by 16 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server client function}}
{{Server client function}}
This function creates a new XML document, which can later be saved to a file by using [[xmlSaveFile]].
This function creates a new XML document, which can later be saved to a file by using [[xmlSaveFile]]. This function will overwrite the file specified if it already exists.
 
{{Note|To prevent memory leaks, ensure each call to [[xmlCreateFile]] has a matching call to [[xmlUnloadFile]]}}
{{Tip|If you do not want to share the content of the created file with other servers, prepend the file path with @ (See [[filepath]] for more information)}}
==Syntax==  
==Syntax==  
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
xmlnode xmlCreateFile ( string filename, string rootNodeName, resource root = getThisResource () )
xmlnode xmlCreateFile ( string filePath, string rootNodeName )
</syntaxhighlight>  
</syntaxhighlight>  
 
{{OOP||[[Xml|XML]]}}
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
xmlnode xmlCreateFile ( string filename, string rootNodeName )
</syntaxhighlight>
 
===Required Arguments===  
===Required Arguments===  
*'''filename:''' The name of the XML file you wish to create.
*'''filePath:''' The [[filepath]] of the file in the following format: '''":resourceName/path"'''. 'resourceName' is the name of the resource the file will be in, and 'path' is the path from the root directory of the resource to the file.
*'''rootNodeName:''' The name of the root node in the XML document.
:For example, if you want to create a file named 'new.xml' in the resource 'ctf', it can be created from another resource this way: ''xmlCreateFile(":ctf/new.xml", "newroot")''.
*'''root:''' Specifies which resource directory to use as root when specifying the filename. Note that if a different resource than default is being accessed, the caller resource needs access to general.ModifyOtherObjects in the ACL.
:If the file is in the current resource, only the file path is necessary, e.g. ''xmlCreateFile("new.xml", "newroot")''.
</section>
:Note that if a different resource than default is being accessed, the caller resource needs access to general.ModifyOtherObjects in the [[ACL]].
*'''rootNodeName:''' the name of the root node in the XML document.


===Returns===
===Returns===
Returns the root [[xmlnode]] object of the new XML file if successful, or ''false'' otherwise.
Returns the root [[xmlnode]] object of the new XML file if successful, or ''false'' otherwise.
==Example==
This example allows a player to use the command 'createfile' to create an .xml file.
<syntaxhighlight lang="lua">
-- Creates a file named "new.xml" with root node "newroot" and childnode "newchild".
function createFileHandler()
  local rootNode = xmlCreateFile("new.xml","newroot")
  local childNode = xmlCreateChild(rootNode, "newchild")
  xmlSaveFile(rootNode)
  xmlUnloadFile(rootNode)
end
addCommandHandler("createfile", createFileHandler)
</syntaxhighlight>


==See Also==
==See Also==
{{XML_functions}}
{{XML_functions}}
 
[[ru:xmlCreateFile]]
[[Category:Needs Example]]

Latest revision as of 15:53, 27 March 2018

This function creates a new XML document, which can later be saved to a file by using xmlSaveFile. This function will overwrite the file specified if it already exists.

[[{{{image}}}|link=|]] Note: To prevent memory leaks, ensure each call to xmlCreateFile has a matching call to xmlUnloadFile
[[{{{image}}}|link=|]] Tip: If you do not want to share the content of the created file with other servers, prepend the file path with @ (See filepath for more information)

Syntax

xmlnode xmlCreateFile ( string filePath, string rootNodeName )

OOP Syntax Help! I don't understand this!

Method: XML(...)


Required Arguments

  • filePath: The filepath of the file in the following format: ":resourceName/path". 'resourceName' is the name of the resource the file will be in, and 'path' is the path from the root directory of the resource to the file.
For example, if you want to create a file named 'new.xml' in the resource 'ctf', it can be created from another resource this way: xmlCreateFile(":ctf/new.xml", "newroot").
If the file is in the current resource, only the file path is necessary, e.g. xmlCreateFile("new.xml", "newroot").
Note that if a different resource than default is being accessed, the caller resource needs access to general.ModifyOtherObjects in the ACL.
  • rootNodeName: the name of the root node in the XML document.

Returns

Returns the root xmlnode object of the new XML file if successful, or false otherwise.

Example

This example allows a player to use the command 'createfile' to create an .xml file.

-- Creates a file named "new.xml" with root node "newroot" and childnode "newchild".
function createFileHandler()
   local rootNode = xmlCreateFile("new.xml","newroot")
   local childNode = xmlCreateChild(rootNode, "newchild")
   xmlSaveFile(rootNode)
   xmlUnloadFile(rootNode)
end

addCommandHandler("createfile", createFileHandler)

See Also