Jump to content

XML Data


Recommended Posts

Hello guys, Again i'm trying to output the name and the Level of every player that login to the server, But wasn't working, here is my code.

Client:

function playersData() 
    if xmlLoadFile(":Data/Data.xml") then 
        outputChatBox("Succesfully loaded!", source) 
        local file = xmlLoadFile(":Data/Data.xml") 
        local dataNode = xmlFindChild(file, "Player", 0) 
        local playerName = getPlayerName(source) 
        local playerLevel = getElementData(source, "LV") or 0 
        xmlNodeSetAttribute(dataNode, "Name", playerName.."Level" .. playerLevel) 
    else 
        local File = xmlCreateFile("Data.xml"," Data") 
        local Child = xmlCreateChild(File, "Player") 
        xmlSaveFile(File) 
        outputChatBox("File created!", source) 
    end 
end 
addEventHandler("onPlayerLogin", getRootElement(), playersData) 

Data.xml

< Data> 
    <Player></Player> 
</ Data> 
  

Link to comment
  • Moderators
function playersData() 
    local rootNote = xmlLoadFile(":Data/Data.xml") --to load it once 
    if not rootNode then 
        rootNode = xmlCreateFile("Data.xml", "Data") 
        xmlCreateChild(rootNode, "Player") 
        xmlSaveFile(rootNode) 
        outputChatBox("File created !", source) 
    end 
    local playerNode = xmlFindChild(rootNote, "Player", 0) 
    local playerName = getPlayerName(source) 
    local playerLevel = getElementData(source, "LV") or 0 
    xmlNodeSetAttribute(playerNode, "Name", playerName) 
    xmlNodeSetAttribute(playerNode, "Level", playerLevel) 
    xmlUnloadFile(rootNode) --don't forget to unload it 
    outputChatBox("Succesfully saved !", source) 
end 
addEventHandler("onPlayerLogin", root, playersData) 

Link to comment

Thanks, But does it create a child "Player" Every time a player login?

Like example:

<Data> 
          <Player>"Nikolai" "Level"</Player> 
</Data> 

And when another player login it will output like this

<Data> 
          <Player>"Nikolai" "Level"</Player> 
          <Player>"Citizen" "Level"</Player> 
</Data> 

And etc..

Link to comment
  • Moderators

Nope, but this one does:

function playersData() 
    local rootNote = xmlLoadFile(":Data/Data.xml") --to load it once 
    if not rootNode then 
        rootNode = xmlCreateFile("Data.xml", "Data") 
        outputChatBox("File created !", source) 
    end 
    local playerName = getPlayerName(source) 
    local playerLevel = getElementData(source, "LV") or 0 
    local playerNode = xmlCreateChild(rootNode, "Player") 
    xmlNodeSetAttribute(playerNode, "Name", playerName) 
    xmlNodeSetAttribute(playerNode, "Level", playerLevel) 
    xmlSaveFile(rootNote) 
    xmlUnloadFile(rootNode) --don't forget to unload it 
    outputChatBox("Succesfully saved !", source) 
end 
addEventHandler("onPlayerLogin", root, playersData) 

<Data> 
    <Player Name="Nikolai", Level="3"></Player> 
    <Player Name="Citizen", Level="1"></Player> 
</Data> 

(It will be easier for you to use the attributes instead of the value of the node)

Link to comment
Nope, but this one does:
function playersData() 
    local rootNote = xmlLoadFile(":Data/Data.xml") --to load it once 
    if not rootNode then 
        rootNode = xmlCreateFile("Data.xml", "Data") 
        outputChatBox("File created !", source) 
    end 
    local playerName = getPlayerName(source) 
    local playerLevel = getElementData(source, "LV") or 0 
    local playerNode = xmlCreateChild(rootNode, "Player") 
    xmlNodeSetAttribute(playerNode, "Name", playerName) 
    xmlNodeSetAttribute(playerNode, "Level", playerLevel) 
    xmlSaveFile(rootNote) 
    xmlUnloadFile(rootNode) --don't forget to unload it 
    outputChatBox("Succesfully saved !", source) 
end 
addEventHandler("onPlayerLogin", root, playersData) 

<Data> 
    <Player Name="Nikolai", Level="3"></Player> 
    <Player Name="Citizen", Level="1"></Player> 
</Data> 

(It will be easier for you to use the attributes instead of the value of the node)

No not working...The Data.xml is not created, No errors in debug.

Link to comment

You are using two different paths:

local rootNote = xmlLoadFile(":Data/Data.xml") --to load it once 
    if not rootNode then 
        rootNode = xmlCreateFile("Data.xml", "Data") 
        outputChatBox("File created !", source) 
    end 

1º path: ":Data/Data.xml"

2º path: "Data.xml"

Link to comment
  • Moderators
xmlLoadFile(":Data/Data.xml") 

Data must be a valid resource's name.

I tried with:

xmlLoadFile("Data.xml") 

And it worked perfectly.

If you wanted to put it in the Data folder that is in the same resource that this code, then use this:

xmlLoadFile("Data/Data.xml") 

Link to comment
function playersData() 
    local rootNote = xmlLoadFile("Data.xml") --to load it once 
    if not rootNode then 
        rootNode = xmlCreateFile("Data.xml", "Data") 
        outputChatBox("File created !", source) 
    end 
    local playerName = getPlayerName(source) 
    local playerLevel = getElementData(source, "LV") or 0 
    local playerNode = xmlCreateChild(rootNode, "Player") 
    xmlNodeSetAttribute(playerNode, "Name", playerName) 
    xmlNodeSetAttribute(playerNode, "Level", playerLevel) 
    xmlSaveFile(rootNote) 
    xmlUnloadFile(rootNode) --don't forget to unload it 
    outputChatBox("Succesfully saved !", source) 
end 
addEventHandler("onPlayerLogin", root, playersData) 

Still not working..

Bad argument 'xmlSaveFile'

Link to comment
function playersData() 
    local rootNote = xmlLoadFile("Data.xml") --to load it once 
    if not rootNode then 
        rootNode = xmlCreateFile("Data.xml", "Data") 
        outputChatBox("File created !", source) 
    end 
    local playerName = getPlayerName(source) 
    local playerLevel = getElementData(source, "LV") or 0 
    local playerNode = xmlCreateChild(rootNode, "Player") 
    xmlNodeSetAttribute(playerNode, "Name", playerName) 
    xmlNodeSetAttribute(playerNode, "Level", playerLevel) 
    xmlSaveFile(rootNote) 
    xmlUnloadFile(rootNode) --don't forget to unload it 
    outputChatBox("Succesfully saved !", source) 
end 
addEventHandler("onPlayerLogin", root, playersData) 

Still not working..

Bad argument 'xmlSaveFile'

Is rootNote returning a file?

Try this:

function playersData() 
    local rootNote; 
    if ( not fileExists ( 'file.xml' ) ) then 
        rootNote = xmlCreateFile("Data.xml", "Data") 
        xmlSaveFile ( rootNote ) 
    else 
        rootNote = xmlLoadFile("Data.xml") 
    end 
     
    local playerName = getPlayerName(source) 
    local playerLevel = getElementData(source, "LV") or 0 
    local playerNode = xmlCreateChild(rootNode, "Player") 
    xmlNodeSetAttribute(playerNode, "Name", playerName) 
    xmlNodeSetAttribute(playerNode, "Level", playerLevel) 
    xmlSaveFile(rootNote) 
    xmlUnloadFile(rootNode) --don't forget to unload it 
    outputChatBox("Succesfully saved !", source) 
end 
addEventHandler("onPlayerLogin", root, playersData) 

 

 

if it is still giving you errors, try using this code that I filled with status messages, and tell us what it outputs:

function playersData() 
    local rootNote = xmlLoadFile("Data.xml") --to load it once 
     
    outputDebugString ( "Loading file.... results: "..tostring ( rootNote ) ) 
     
    if not rootNode then 
        rootNode = xmlCreateFile("Data.xml", "Data") 
        xmlSaveFile ( rootNode ) 
        outputDebugString ( "File created and saved... Result: "..tostring ( rootNote ) ) 
    end 
     
    local playerName = getPlayerName(source) 
    local playerLevel = getElementData(source, "LV") or 0 
    local playerNode = xmlCreateChild(rootNode, "Player") 
    outputDebugString ( "Creating 'Player' child... result: "..tostring ( playerNode ) ) 
    xmlNodeSetAttribute(playerNode, "Name", playerName) 
    xmlNodeSetAttribute(playerNode, "Level", playerLevel) 
    local save = xmlSaveFile(rootNote) 
    local unload = xmlUnloadFile(rootNode) --don't forget to unload it 
    outputDebugString ( "File saved and unloaded. Save return: "..tostring(save).." | Unload return: "..tostring ( unload ) ) 
end 
addEventHandler("onPlayerLogin", root, playersData) 

Link to comment
function playersData() 
    local rootNote = xmlLoadFile("Data.xml") --to load it once 
    if not rootNode then 
        rootNode = xmlCreateFile("Data.xml", "Data") 
        outputChatBox("File created !", source) 
    end 
    local playerName = getPlayerName(source) 
    local playerLevel = getElementData(source, "LV") or 0 
    local playerNode = xmlCreateChild(rootNode, "Player") 
    xmlNodeSetAttribute(playerNode, "Name", playerName) 
    xmlNodeSetAttribute(playerNode, "Level", playerLevel) 
    xmlSaveFile(rootNote) 
    xmlUnloadFile(rootNode) --don't forget to unload it 
    outputChatBox("Succesfully saved !", source) 
end 
addEventHandler("onPlayerLogin", root, playersData) 

Still not working..

Bad argument 'xmlSaveFile'

Is rootNote returning a file?

Try this:

function playersData() 
    local rootNote; 
    if ( not fileExists ( 'file.xml' ) ) then 
        rootNote = xmlCreateFile("Data.xml", "Data") 
        xmlSaveFile ( rootNote ) 
    else 
        rootNote = xmlLoadFile("Data.xml") 
    end 
     
    local playerName = getPlayerName(source) 
    local playerLevel = getElementData(source, "LV") or 0 
    local playerNode = xmlCreateChild(rootNode, "Player") 
    xmlNodeSetAttribute(playerNode, "Name", playerName) 
    xmlNodeSetAttribute(playerNode, "Level", playerLevel) 
    xmlSaveFile(rootNote) 
    xmlUnloadFile(rootNode) --don't forget to unload it 
    outputChatBox("Succesfully saved !", source) 
end 
addEventHandler("onPlayerLogin", root, playersData) 

 

 

if it is still giving you errors, try using this code that I filled with status messages, and tell us what it outputs:

function playersData() 
    local rootNote = xmlLoadFile("Data.xml") --to load it once 
     
    outputDebugString ( "Loading file.... results: "..tostring ( rootNote ) ) 
     
    if not rootNode then 
        rootNode = xmlCreateFile("Data.xml", "Data") 
        xmlSaveFile ( rootNode ) 
        outputDebugString ( "File created and saved... Result: "..tostring ( rootNote ) ) 
    end 
     
    local playerName = getPlayerName(source) 
    local playerLevel = getElementData(source, "LV") or 0 
    local playerNode = xmlCreateChild(rootNode, "Player") 
    outputDebugString ( "Creating 'Player' child... result: "..tostring ( playerNode ) ) 
    xmlNodeSetAttribute(playerNode, "Name", playerName) 
    xmlNodeSetAttribute(playerNode, "Level", playerLevel) 
    local save = xmlSaveFile(rootNote) 
    local unload = xmlUnloadFile(rootNode) --don't forget to unload it 
    outputDebugString ( "File saved and unloaded. Save return: "..tostring(save).." | Unload return: "..tostring ( unload ) ) 
end 
addEventHandler("onPlayerLogin", root, playersData) 

I will try it, Anyway the location where i want this file to get created is in the same directory where the script is.

Link to comment
  • Moderators

Here is my code again with the fix of the mistake I made (thanks Solidsnake) and now storing that file in your resource folder:

function playersData() 
    local fileName = "Data.xml" 
    local rootNote = xmlLoadFile(fileName) --to load it once 
    if not rootNode then 
        rootNode = xmlCreateFile(fileName, "Data") 
        outputChatBox("File created !", source) 
    end 
    local playerName = getPlayerName(source) 
    local playerLevel = getElementData(source, "LV") or 0 
    local playerNode = xmlCreateChild(rootNode, "Player") 
    xmlNodeSetAttribute(playerNode, "Name", playerName) 
    xmlNodeSetAttribute(playerNode, "Level", playerLevel) 
    xmlSaveFile(rootNote) 
    xmlUnloadFile(rootNode) --don't forget to unload it 
    outputChatBox("Succesfully saved !", source) 
end 
addEventHandler("onPlayerLogin", root, playersData) 

By the way, the code should have work once properly. The fact that it didn't work for you let me think you have an error in the same file.

Please check the errors in the console when you start that resource.

(Note this is a server side script)

And as I said, I tested it and it worked !

Link to comment
  • Moderators

Myeah just spotted that I wrote rootNote instead of rootNode at some places.

I said it was working because the outputs were good. But the xmlSaveFile couldn't work (that's why it was never saved in the folder):

function playersData() 
    local fileName = "Data.xml" 
    local rootNode = xmlLoadFile(fileName) --to load it once 
    if not rootNode then 
        rootNode = xmlCreateFile(fileName, "Data") 
        if rootNode then 
            outputChatBox("File created !", source) 
        else 
            outputChatBox("Failed to create the file !", source) 
            return 
        end 
    end 
    local playerName = getPlayerName(source) 
    local playerLevel = getElementData(source, "LV") or 0 
    local playerNode = xmlCreateChild(rootNode, "Player") 
    xmlNodeSetAttribute(playerNode, "Name", playerName) 
    xmlNodeSetAttribute(playerNode, "Level", playerLevel) 
    xmlSaveFile(rootNode) 
    xmlUnloadFile(rootNode) 
    outputChatBox("Succesfully saved !", source) 
end 
addEventHandler("onPlayerLogin", root, playersData) 

Should be the last time I update this script.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...