Jump to content

How to find and modify a child in XML


'LinKin

Recommended Posts

Hello,

I'm trying to modify a child into a xml file that stores some teams.

How could I for example; find and change an attribute?

I got this:

<team name="Street Warriors" tag="|SW|" color="#00FFFF" aclGroup="GMC" required="both"></team> 
    <team name="Team 1" tag="|T1|" color="#990000" aclGroup="-" required="tag"></team> 
    <team name="Team 2" tag="|T2|" color="#009900" aclGroup="-" required="tag"></team> 
    <team name="Team 3" tag="|T3|" color="#000099" aclGroup="-" required="tag"></team> 

So if I wanted to change 'Team 2' color to #FF00FF so that it becomes like this:

<team name="Street Warriors" tag="|SW|" color="#00FFFF" aclGroup="GMC" required="both"></team> 
    <team name="Team 1" tag="|T1|" color="#990000" aclGroup="-" required="tag"></team> 
    <team name="Team 2" tag="|T2|" color="#FF00FF" aclGroup="-" required="tag"></team> 
    <team name="Team 3" tag="|T3|" color="#000099" aclGroup="-" required="tag"></team> 

how could I do it?

I want to make the search based on the 'name' attribute.

Link to comment
  • MTA Team
  
function saveTeamColor(file, team, color) 
    local rootnode = xmlLoadFile(file) 
    if (not rootnode) then 
        return 
    end 
    local index, node = 0, false 
    repeat 
        node = xmlFindChild(rootnode, "team", index) 
        if (node) then 
            local name = xmlNodeGetAttribute(node, "name") 
            if (name and name == team) then 
                xmlNodeSetAttribute(node, "name", tostring(color)) 
                xmlUnloadFile(rootnode) 
                break 
            end 
            index = index + 1 
        end 
    until (node) 
    xmlUnloadFile(rootnode) 
end 
  

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