Skip to content

Commit 08b694b

Browse files
committed
XML functions
1 parent 4a8251c commit 08b694b

39 files changed

+867
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local config = xmlLoadFile("config.xml")
2+
-- If the file doesn't exist, we don't proceed.
3+
if (not config) then
4+
return
5+
end
6+
7+
-- Create a copy of xml structure in memory
8+
local newFile = xmlCopyFile(config, "copy/copy-config.xml")
9+
if (newFile) then
10+
-- Write this new copy to a filesystem
11+
xmlSaveFile(newFile)
12+
end
13+
-- Unload config xml node from memory if it will not be used anytime soon
14+
xmlUnloadFile(config)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Creates a file named "new.xml" with root node "newroot" and childnode "newchild".
2+
function createFileHandler()
3+
local RootNode = xmlCreateFile("new.xml"," newroot")
4+
if (not RootNode) then
5+
return
6+
end
7+
8+
local NewNode = xmlCreateChild(RootNode, "newchild")
9+
if (not NewNode) then
10+
return
11+
end
12+
13+
xmlSaveFile(RootNode)
14+
xmlUnloadFile(RootNode)
15+
end
16+
addCommandHandler("createfile", createFileHandler)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Creates a file named "new.xml" with root node "newroot" and childnode "newchild".
2+
function createFileHandler()
3+
local RootNode = xmlCreateFile("new.xml"," newroot")
4+
if (not RootNode) then
5+
return
6+
end
7+
8+
local NewNode = xmlCreateChild(RootNode, "newchild")
9+
if (not NewNode) then
10+
return
11+
end
12+
13+
xmlSaveFile(RootNode)
14+
xmlUnloadFile(RootNode)
15+
end
16+
addCommandHandler("createfile", createFileHandler)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
addEventHandler("onResourceStart", resourceRoot, function()
2+
xml = xmlLoadFile("test.xml")
3+
if (not xml) then
4+
xml = xmlCreateFile("test.xml", "root")
5+
xmlCreateChild(xml, "node")
6+
xmlSaveFile(xml)
7+
end
8+
end)
9+
10+
addCommandHandler("delnode", function(player)
11+
local node = xmlFindChild(xml, "node", 0)
12+
if (not node) then
13+
xmlUnloadFile(xml)
14+
return
15+
end
16+
17+
xmlDestroyNode(node)
18+
xmlSaveFile(xml)
19+
xmlUnloadFile(xml)
20+
outputChatBox("You destroyed the node!", player, 0, 255, 0, false)
21+
end)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local rootNode = xmlLoadFile("test.xml")
2+
if (not rootNode) then
3+
return
4+
end
5+
6+
local optionsNode = xmlFindChild(rootNode, "options", 0)
7+
if (optionsNode) then
8+
local instructionsNode = xmlFindChild(optionsNode, "instructions", 0)
9+
10+
if (instructionsNode) then
11+
local instructions = xmlNodeGetValue(instructionsNode)
12+
outputChatBox(instructions)
13+
end
14+
end
15+
16+
xmlUnloadFile(rootNode)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local node = xmlLoadFile ( ":ctv/settings.xml" )
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local rootNode = xmlLoadString("<animals test='x'><wolf name='timmy'></wolf> <fox name='luxy'></fox></animals>")
2+
3+
if rootNode then
4+
local rootAttributes = xmlNodeGetAttributes(rootNode)
5+
print("Root Node", "Name: "..xmlNodeGetName(rootNode), "Attributes :"..toJSON(rootAttributes))
6+
7+
local children = xmlNodeGetChildren(rootNode)
8+
9+
for i, childNode in ipairs(children) do
10+
local attributes = xmlNodeGetAttributes(childNode)
11+
print("Child #"..i, "Name: "..xmlNodeGetName(childNode), "Attributes :"..toJSON(attributes))
12+
end
13+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local xml = getResourceConfig("settings.xml") -- load XML file and get its root element
2+
local carmodel = xmlNodeGetAttribute(xml, "model") -- get attribute of root element
3+
local carX = xmlNodeGetAttribute(xml, "posX")
4+
local carY = xmlNodeGetAttribute(xml, "posY")
5+
local carZ = xmlNodeGetAttribute(xml, "posZ")
6+
local carA = xmlNodeGetAttribute(xml, "rot")
7+
createVehicle(carmodel, tonumber(carX), tonumber(carY), tonumber(carZ), 0.0, 0.0, tonumber(carA))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local meta = xmlLoadFile("meta.xml")
2+
local info = xmlFindChild(meta, "info", 0)
3+
4+
if (info) then
5+
local attrs = xmlNodeGetAttributes(info)
6+
for name,value in pairs (attrs) do
7+
outputConsole(name.." = "..value)
8+
end
9+
end
10+
11+
xmlUnloadFile(meta)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
addEventHandler("onResourceStart", getResourceRootElement(),
2+
function()
3+
local xml = xmlLoadFile("welcome.xml") -- open the XML file
4+
local messageNodes = xmlNodeGetChildren(xml) -- get all child nodes of the root node (<messages>)
5+
g_WelcomeMessages = {} -- create a new global variable to store the welcome messages
6+
for i,node in ipairs(messageNodes) do -- loop over all the message nodes
7+
g_WelcomeMessages[i] = xmlNodeGetValue(node) -- retrieve the text in each node
8+
end
9+
xmlUnloadFile(xml) -- close the XML file
10+
end
11+
)
12+
13+
addEventHandler("onPlayerJoin", getRootElement(),
14+
function()
15+
local numMessages = #g_WelcomeMessages -- get the number of messages
16+
local message = g_WelcomeMessages[math.random(numMessages)] -- pick a random message
17+
outputChatBox(message, source, 0, 255, 0) -- display it to the joining player
18+
end
19+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local xml = xmlCreateFile("test.xml","test")
2+
if (not xml) then
3+
return
4+
end
5+
6+
local xmlNode = xmlCreateChild(xml, "nextTest")
7+
local xmlNodeName = xmlNodeGetName(xmlNode)
8+
xmlUnloadFile(xml)
9+
10+
outputConsole(xmlNodeName) -- This should output "nextTest"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local xmlFile = xmlLoadFile("exampleFile.xml") -- Open a file that we have already created
2+
if (xmlFile) then -- If it's indeed opened
3+
local node = xmlFindChild(xmlFile, "somesubnode", 0) -- Find our first sub node
4+
local success = xmlNodeGetValue(node) -- Get the value of it
5+
xmlUnloadFile(xmlFile)
6+
7+
if (success) then -- Check if it was successful
8+
outputChatBox (tostring(success)) -- Output "somevalue" to the chatbox
9+
end
10+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function ClientResourceStart ()
2+
if (source ~= getResourceRootElement()) then return end --This event will happen with any resource start, isolate it to this resource
3+
xmlRootTree = xmlLoadFile ("userSettings.xml") --Attempt to load the xml file
4+
5+
if (xmlRootTree) then -- If the xml loaded then...
6+
xmlHudBranch = xmlFindChild(xmlRootTree,"hud_display",0) -- Find the hud sub-node
7+
xmlBindsBranch = xmlFindChild(xmlRootTree,"hud_binds",0) -- Find the binds sub-node
8+
outputChatBox("XML Found and Loaded")
9+
else -- If the xml does not exist then...
10+
xmlRootTree = xmlCreateFile("userSettings.xml", "root") -- Create the xml file
11+
xmlHudBranch = xmlCreateChild(xmlRootTree, "hud_display") -- Create the hud sub-node under the root node
12+
xmlBindsBranch = xmlCreateChild(xmlRootTree, "hud_binds")-- Create the binds sub-node under the root node
13+
xmlNodeSetValue(xmlCreateChild(xmlHudBranch, "IconSizeX"), "60") --Create sub-node values under the hud sub-node
14+
xmlNodeSetValue(xmlCreateChild(xmlHudBranch, "IconSizeY"), "60") --Create sub-node values under the hud sub-node
15+
xmlNodeSetValue(xmlCreateChild(xmlBindsBranch, "weaponSlot0"), "tab") --Create sub-node values under the binds sub-node
16+
xmlNodeSetValue(xmlCreateChild(xmlBindsBranch, "weaponSlot1"), "1") --Create sub-node values under the binds sub-node
17+
outputChatBox("XML Created")
18+
end
19+
20+
--Retrieve a single sub-node name or value
21+
outputChatBox("Node name: "..xmlNodeGetName(xmlFindChild(xmlHudBranch,"IconSizeX",0)), 0, 0, 255) --blue outputs
22+
outputChatBox("Node Value: "..xmlNodeGetValue(xmlFindChild(xmlHudBranch,"IconSizeX",0)), 0, 0, 255) --blue outputs
23+
24+
--Retrieve multiple sub-node names or values
25+
xmlHudChildrenTable = xmlNodeGetChildren(xmlHudBranch) --Create a table of this branch's children
26+
for i,node in pairs(xmlHudChildrenTable) do --Loop through the branch's children for sub-nodes
27+
outputChatBox("Node name: "..xmlNodeGetName(node), 0, 255, 0) --green outputs
28+
outputChatBox("Node Value: "..xmlNodeGetValue(node), 0, 255, 0) --green outputs
29+
end
30+
end
31+
addEventHandler("onClientResourceStart", root, ClientResourceStart)
32+
33+
function ClientResourceStop ()
34+
if (source ~= getResourceRootElement()) then return end --This event will happen with any resource stop, isolate it to this resource
35+
36+
xmlSaveFile(xmlRootTree) --Save the xml from memory for use next time
37+
xmlUnloadFile(xmlRootTree) --Unload the xml from memory
38+
outputChatBox( "Saved and unloaded the XML.")
39+
end
40+
addEventHandler("onClientResourceStop", root, ClientResourceStop)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function changeConfigMarkerColor(thePlayer, command, r, g, b)
2+
local config = xmlLoadFile("config.xml")
3+
if (not config) then
4+
return
5+
end
6+
7+
local markernode = xmlFindChild(config, "markers", 0)
8+
if (markernode) then
9+
xmlNodeSetAttribute(markernode, "color", r .. "," .. g .. "," .. b)
10+
xmlNodeSetAttribute(markernode, "foo", nil) -- remove 'foo' attribute
11+
end
12+
13+
xmlSaveFile(config)
14+
xmlUnloadFile(config)
15+
end
16+
addCommandHandler("markercolor", changeConfigMarkerColor)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
local xml = xmlCreateFile("fileName.xml","Test")
2+
if (not xml) then
3+
return
4+
end
5+
6+
local xmlNode = xmlCreateChild(xml,"Test1")
7+
local xmlNodeName = xmlNodeGetName(xmlNode)
8+
if (xmlNodeName == "Test1") then
9+
xmlNodeSetName(xmlNode, "Test2")
10+
end
11+
12+
xmlUnloadFile(xml)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local xmlFile = xmlLoadFile("exampleFile.xml") -- Open a file already created
2+
if (xmlFile) then -- If it's indeed opened
3+
local node = xmlCreateChild(xmlFile, "somesubnode") -- Create a new subnode
4+
local success = xmlNodeSetValue(node, "somevalue") -- Set the value of it
5+
6+
if (success) then -- Check if it was successful
7+
xmlSaveFile(xmlFile) -- Save the file
8+
end
9+
xmlUnloadFile(xmlFile)
10+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function ClientResourceStart ()
2+
xmlRootTree = xmlLoadFile ( "userSettings.xml" ) --Attempt to load the xml file
3+
4+
if xmlRootTree then -- If the xml loaded then...
5+
xmlHudBranch = xmlFindChild(xmlRootTree,"hud_display",0) -- Find the hud sub-node
6+
xmlBindsBranch = xmlFindChild(xmlRootTree,"hud_binds",0) -- Find the binds sub-node
7+
outputChatBox ( "XML Found and Loaded" )
8+
else -- If the xml does not exist then...
9+
xmlRootTree = xmlCreateFile ( "userSettings.xml", "root" ) -- Create the xml file
10+
xmlHudBranch = xmlCreateChild ( xmlRootTree, "hud_display" ) -- Create the hud sub-node under the root node
11+
xmlBindsBranch = xmlCreateChild ( xmlRootTree, "hud_binds" )-- Create the binds sub-node under the root node
12+
xmlNodeSetValue (xmlCreateChild ( xmlHudBranch, "IconSizeX"), "60" ) --Create sub-node values under the hud sub-node
13+
xmlNodeSetValue (xmlCreateChild ( xmlHudBranch, "IconSizeY"), "60" ) --Create sub-node values under the hud sub-node
14+
xmlNodeSetValue (xmlCreateChild ( xmlBindsBranch, "weaponSlot0"), "tab" ) --Create sub-node values under the binds sub-node
15+
xmlNodeSetValue (xmlCreateChild ( xmlBindsBranch, "weaponSlot1"), "1" ) --Create sub-node values under the binds sub-node
16+
outputChatBox ( "XML Created" )
17+
end
18+
19+
--Retrieve a single sub-node name or value
20+
outputChatBox( "Node name: "..xmlNodeGetName (xmlFindChild(xmlHudBranch,"IconSizeX",0)), 0, 0, 255 ) --blue outputs
21+
outputChatBox( "Node Value: "..xmlNodeGetValue (xmlFindChild(xmlHudBranch,"IconSizeX",0)), 0, 0, 255 ) --blue outputs
22+
23+
--Retrieve multiple sub-node names or values
24+
xmlHudChildrenTable = xmlNodeGetChildren ( xmlHudBranch ) --Create a table of this branch's children
25+
for i,node in pairs(xmlHudChildrenTable ) do --Loop through the branch's children for sub-nodes
26+
outputChatBox( "Node name: "..xmlNodeGetName (node), 0, 255, 0 ) --green outputs
27+
outputChatBox( "Node Value: "..xmlNodeGetValue(node), 0, 255, 0 ) --green outputs
28+
end
29+
end
30+
addEventHandler ( "onClientResourceStart", resourceRoot, ClientResourceStart )
31+
32+
function ClientResourceStop ()
33+
xmlSaveFile ( xmlRootTree ) --Save the xml from memory for use next time
34+
xmlUnloadFile ( xmlRootTree ) --Unload the xml from memory
35+
outputChatBox ( "Saved and unloaded the XML." )
36+
end
37+
addEventHandler ( "onClientResourceStop", resourceRoot, ClientResourceStop )
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Creates a file named "new.xml" with root node "newroot" and childnode "newchild".
2+
function createFileHandler()
3+
local RootNode = xmlCreateFile("new.xml"," newroot")
4+
local NewNode = xmlCreateChild(RootNode, "newchild")
5+
xmlSaveFile(RootNode)
6+
xmlUnloadFile(RootNode)
7+
end
8+
addCommandHandler("createfile", createFileHandler)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local xml = xmlLoadFile("config.xml")
2+
if (not xml) then
3+
return
4+
end
5+
6+
local markernode = xmlFindChild(xml, "markers", 0)
7+
xmlNodeSetAttribute(markernode, "color", "0,0,200")
8+
9+
xmlSaveFile(xml)
10+
xmlUnloadFile(xml)

functions/XML/xmlCopyFile.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
shared: &shared
2+
name: 'xmlCopyFile'
3+
notes:
4+
- type: 'warning'
5+
content: |
6+
To prevent memory leaks, ensure each call to **xmlCopyFile** has a matching call to [xmlUnloadFile](/xmlUnloadFile).
7+
oop:
8+
entity: xmlnode
9+
method: copy
10+
description: |
11+
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](/xmlSaveFile) is called.
12+
parameters:
13+
- name: 'nodeToCopy'
14+
type: 'xmlnode'
15+
description: "The [xmlnode](/xmlnode) that is to be copied to a new document."
16+
returns:
17+
description: |
18+
Returns the [xmlnode](/xmlnode) of the copy if the node was successfully copied, **false** if invalid arguments were passed.
19+
values:
20+
- type: 'xmlnode|false'
21+
name: 'xmlnode'
22+
examples:
23+
- path: 'examples/xmlCopyFile-1.lua'
24+
description: |
25+
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.

functions/XML/xmlCreateChild.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
shared: &shared
2+
name: 'xmlCreateChild'
3+
oop:
4+
entity: xmlnode
5+
method: createChild
6+
description: |
7+
This function creates a new child node under an XML node.
8+
parameters:
9+
- name: 'parentNode'
10+
type: 'xmlnode'
11+
description: "The [xmlnode](/xmlnode) you want to create a new child node under."
12+
- name: 'tagName'
13+
type: 'string'
14+
description: 'The type of the child node that will be created.'
15+
returns:
16+
description: |
17+
Returns the created [xmlnode](/xmlnode) if successful, false otherwise.
18+
values:
19+
- type: 'xmlnode|false'
20+
name: 'xmlnode'
21+
22+
client:
23+
<<: *shared
24+
examples:
25+
- path: 'examples/xmlCreateChild-1.lua'
26+
description: |
27+
This example allows a player to use the command **createfile** to create an .xml file.

0 commit comments

Comments
 (0)