Jump to content

HELP,XML problem


TheIceman1

Recommended Posts

Why this dont delete clan from "clans.xml"?

function destroyclan () 
local clan = getElementData ( source, "clan" ) 
removeElementData ( source, "clan" ) 
    local clans = xmlLoadFile ("clans.xml") 
    local clansroot = xmlFindChild (clans,"clans",0) 
   if ( clansroot ) then 
    for i,v in ipairs (xmlNodeGetChildren(clansroot)) do 
      local xmlnumber = xmlNodeGetAttribute (v,"clan") 
      xmlDestroyNode ( v ) 
      xmlSaveFile ( clans ) 
      xmlUnloadFile ( clans ) 
end 
end 
end 
addEvent ( "destroyclan", true ) 
addEventHandler ( "destroyclan", root, destroyclan ) 

XML

    "FBI">

Link to comment

You're trying to delete the children of this node:

clan="FBI">>

Since it contains no children, nothing is deleted. It happens this way because xmlFileLoad returns this node:

>...>

And then you're getting its first child (FBI clan) as 'clansroot', then looping through its children (which don't exist, therefore nothing is removed). You're supposed to loop through the children of 'clans' instead.

In addition, you're saving and unloading the XML file inside the loop - that's wrong. You should do this after the loop. If you unload the file inside the loop, it won't be available for the next iteration, causing an error. Saving the file repeatedly will have no effect on the result, but doing so will needlessly reduce the performance.

Link to comment
You're trying to delete the children of this node:
clan="FBI">>

Since it contains no children, nothing is deleted. It happens this way because xmlFileLoad returns this node:

>...>

And then you're getting its first child (FBI clan) as 'clansroot', then looping through its children (which don't exist, therefore nothing is removed). You're supposed to loop through the children of 'clans' instead.

In addition, you're saving and unloading the XML file inside the loop - that's wrong. You should do this after the loop. If you unload the file inside the loop, it won't be available for the next iteration, causing an error. Saving the file repeatedly will have no effect on the result, but doing so will needlessly reduce the performance.

I absolutely dont understand...

Link to comment

Don't make as a child of . This is confusing.

<clans> 
    <clan name="FBI"></clan> 
</clans> 

- 1 the most important thing you do but should not do is close the file inside a loop which was opened outside the loop. Do xmlSaveFile and xmlUnloadFile after you've done your operations.

- You're trying to delete all the children.

- Why do you use getElementData at the 2nd line if you don't even use the returned value anywhere in the code?

- Check if the clan node (a child of ) is the same name as the clan you're trying to delete/destroy.

Link to comment

Why this dont delete clan from "clans.xml"?

function destroyclan () 
local clan = getElementData ( source, "clan" ) 
removeElementData ( source, "clan" ) 
   local clans = xmlLoadFile ("clans.xml") 
    local clansroot = xmlFindChild (clans,"clans",0) 
    if (clansroot) then 
    for i,v in ipairs (xmlNodeGetChildren(clansroot)) do 
      local xmlnumber = xmlNodeGetAttribute (v,"name") 
         if ( xmlnumber == clan ) then 
           xmlDestroyNode ( v ) 
     end 
      xmlSaveFile ( clans ) 
      xmlUnloadFile ( clans ) 
end 
end 
end 
addEvent ( "destroyclan", true ) 
addEventHandler ( "destroyclan", root, destroyclan ) 

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