Jump to content

HELP,XML problem


TheIceman1

Recommended Posts

Posted

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

Posted

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.

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

Posted

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.

Posted

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 ) 

Posted

xmlLoadFile already returns pointer to the root of the xml file, so you don't need to search 'clans' node again with xmlFindChild. Try to comment the 5th line and rename 'clans' variable to 'clansroot' in the 4th line and see if that helps.

Posted
xmlLoadFile already returns pointer to the root of the xml file, so you don't need to search 'clans' node again with xmlFindChild. Try to comment the 5th line and rename 'clans' variable to 'clansroot' in the 4th line and see if that helps.

Working,Thanks!

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