XmlCopyFile: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
(18 intermediate revisions by 10 users not shown)
Line 2: Line 2:
{{Server client function}}
{{Server client function}}
This function copies all contents of a certain node in a XML document to a new document file, so the copied node becomes the new file's root node.
This function copies all contents of a certain node in a XML document to a new document file, so the copied node becomes the new file's root node.
 
The new file will not be saved to file system until [[xmlSaveFile]]() is called
{{Note|To prevent memory leaks, ensure each call to [[xmlCopyFile]] has a matching call to [[xmlUnloadFile]]}}
==Syntax==  
==Syntax==  
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
xmlnode xmlCopyFile ( xmlnode nodeToCopy, string newFilename, [ resource inResource = getThisResource() ] )
xmlnode xmlCopyFile ( xmlnode nodeToCopy, string newFilePath )
</syntaxhighlight>  
</syntaxhighlight>  
 
{{OOP||[[xmlnode]]:copy||}}
===Required Arguments===  
===Required Arguments===  
*'''nodeToCopy:''' the [[xmlnode]] that is to be copied to a new document.
*'''nodeToCopy:''' the [[xmlnode]] that is to be copied to a new document.
*'''newFilename:''' the filename for the new XML document.
*'''newFilePath:''' the path of the file that is to be created, in the following format: '''":resourceName/path"'''. 'resourceName' is the name of the resource the file is in, and 'path' is the path from the root directory of the resource to the file.
:For example, to create a file named 'newfile.xml' with myNode as the root node in the resource 'ctf', it can be done from another resource this way: ''xmlCopyFile(myNode, ":ctf/newfile.xml")''.
:If the file is to be in the current resource, only the file path is necessary, e.g. ''xmlCopyFile(myNode, "newfile.xml")''.


===Optional Arguments===
===Returns===
{{OptionalArg}}
Returns the [[xmlnode]] of the copy if the node was successfully copied, ''false'' if invalid arguments were passed.
*'''inResource:''' the [[resource]] whose directory will be used as the root for the filename specified. Note that if a different resource than default is being accessed, the caller resource needs access to general.ModifyOtherObjects in the [[ACL]].
</section>


<section name="Client" class="client" show="true">
==Example==  
In this example we will load an xml file (in the example config.xml) and create a copy in a new folder with the name of copy-config.xml:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
xmlnode xmlCopyFile ( xmlnode nodeToCopy, string newFilename )
local config = xmlLoadFile("config.xml")
-- create a copy of xml structure in memory
local newFile = xmlCopyFile(config, "copy/copy-config.xml")
if newFile then
  -- write this new copy to a filesystem
  xmlSaveFile(newFile)
end
-- unload config xml node from memory if it will not be used anytime soon
xmlUnloadFile(config)
</syntaxhighlight>
</syntaxhighlight>
===Required Arguments===
*'''nodeToCopy:''' the [[xmlnode]] that is to be copied to a new document.
*'''newFilename:''' the filename for the new XML document.
</section>
===Returns===
Returns a [[xmlnode]] if the node was successfully copied, ''false'' if invalid arguments were passed.
==Example==
[[Category:Needs Example]]


==See Also==
==See Also==
{{XML_functions}}
{{XML_functions}}
[[ru:xmlCopyFile]]

Latest revision as of 23:15, 13 February 2015

This function copies all contents of a certain node in a XML document to a new document file, so the copied node becomes the new file's root node. The new file will not be saved to file system until xmlSaveFile() is called

[[{{{image}}}|link=|]] Note: To prevent memory leaks, ensure each call to xmlCopyFile has a matching call to xmlUnloadFile

Syntax

xmlnode xmlCopyFile ( xmlnode nodeToCopy, string newFilePath )

OOP Syntax Help! I don't understand this!

Method: xmlnode:copy(...)


Required Arguments

  • nodeToCopy: the xmlnode that is to be copied to a new document.
  • newFilePath: the path of the file that is to be created, in the following format: ":resourceName/path". 'resourceName' is the name of the resource the file is in, and 'path' is the path from the root directory of the resource to the file.
For example, to create a file named 'newfile.xml' with myNode as the root node in the resource 'ctf', it can be done from another resource this way: xmlCopyFile(myNode, ":ctf/newfile.xml").
If the file is to be in the current resource, only the file path is necessary, e.g. xmlCopyFile(myNode, "newfile.xml").

Returns

Returns the xmlnode of the copy if the node was successfully copied, false if invalid arguments were passed.

Example

In this example we will load an xml file (in the example config.xml) and create a copy in a new folder with the name of copy-config.xml:

local config = xmlLoadFile("config.xml")
-- create a copy of xml structure in memory
local newFile = xmlCopyFile(config, "copy/copy-config.xml")
if newFile then
  -- write this new copy to a filesystem
  xmlSaveFile(newFile)
end
-- unload config xml node from memory if it will not be used anytime soon
xmlUnloadFile(config)

See Also