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+
)

0 commit comments

Comments
 (0)