Jump to content

[HELP] Change value of XML file


[M]ister

Recommended Posts

Hello I am creating a system for teams and plan to use xml to save data from gangs. But I'm having some difficulty using it and I would do the following: Well I'm in the team 'Gang1' and type in the command ex: '/ gang tag ' and with this he change the value 'tag' of the xml file .

gangs.xml

<teams> 
    <team name="Gang1" tag="TAG1" color="#FF9e00"></team> 
    <team name="Gang2" tag="TAG2" color="#00FF00"></team> 
    <team name="Gang3" tag="TAG3" color="#777777"></team> 
</teams> 

script.lua

addCommandHandler("gang", function(player,cmd,subcmd,newtag) 
    if subcmd == "tag" then 
    -- help me with code ? 
    end 
end) 

Could anyone help me with this?

Link to comment

It changes the value of the 'tag' in XML, but prints the message 'Error' twice

addCommandHandler("gang", function(player,cmd,subcmd,newtag) 
    if subcmd == "tag" then 
        local xml = xmlLoadFile("gangs.xml") 
        local children = xmlNodeGetChildren(xml) 
        teams = {} 
        for i,nodes in pairs(children) do 
            teams[i] = xmlNodeGetAttribute(nodes,"name") 
            if teams[i] == getTeamName(getPlayerTeam(player)) then 
                xmlNodeSetAttribute(nodes,"tag",newtag) 
            else 
                outputChatBox("Error") 
            end 
        end 
        xmlSaveFile(xml) 
        xmlUnloadFile(xml) 
    end 
end) 

Link to comment

14080706524726412.png

addCommandHandler("gang", function(player,cmd,subcmd,newtag) 
    if subcmd == "tag" then 
        local xml = xmlLoadFile("gangs.xml") 
        local children = xmlNodeGetChildren(xml) 
        teams = {} 
        for i,nodes in pairs(children) do 
            teams[i] = xmlNodeGetAttribute(nodes,"name") 
            teamName = getTeamName(getPlayerTeam(player)) 
            outputChatBox("Team name in xml: "..teams[i].."  Team name of player: "..teamName) 
            if teams[i] == teamName then 
                xmlNodeSetAttribute(nodes,"tag",newtag) 
                --outputChatBox("You changed the tag") 
            else 
                --outputChatBox("Error") 
            end 
        end 
        xmlSaveFile(xml) 
        xmlUnloadFile(xml) 
    end 
end) 

Link to comment

Why do you store the team anyway? Couldn't you just go through all nodes, get the name, and compare with the team name?

  
if xmlNodeGetAttribute(nodes,"name") == teamName then 
   xmlNodeSetAttribute(nodes,"tag",newtag) 
   return 
end 
  

Link to comment
Why do you store the team anyway? Couldn't you just go through all nodes, get the name, and compare with the team name?
  
if xmlNodeGetAttribute(nodes,"name") == teamName then 
   xmlNodeSetAttribute(nodes,"tag",newtag) 
   return 
end 
  

Because it can return false. And why would you check something that failed? Check or it's false and if so output it, always handy for typos in XML file.

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...