Jump to content

Looping Thru An xml


ArmedGuy

Recommended Posts

Hokay. Im Trying To Get A Script To Search For a Specific Subnode and Get The Attributes from it But as all the Subnodes have the same name i gotta make it loop through, and i have no idea how to do that.

Heres The .lua(now it looks messed up as i tried to make something, didnt work thou)

local saveFile = xmlLoadFile ("playerstats.xml")
local statscheck = 0
local statssave = 0
 
function onPlayerQuit ( source )
xmlLoadFile ("playerstats.xml")
outputChatBox( "Saving Data", source )
if ( saveFile ) then
local subnode = xmlFindSubNode( saveFile, "users", 0 )
local playeraccount = getClientName ( source )
if ( playeraccount ) then
local stats = xmlFindSubNode( subnode, "player", statssave )
local name = xmlNodeGetAttribute( stats ,"name")
if ((stats) and (name ==""..playeraccount)) then
local playermoney = getPlayerMoney ( source )
local playerskin = getPlayerSkin ( source )
local money = xmlNodeSetAttribute( stats, "money", playermoney )
local skin = xmlNodeSetAttribute( stats, "skin", playerskin )
xmlSaveFile ( saveFile )
else
				statssave = statssave + 1
end
end
else
outputChatBox("File Not Found", source )
end
end
 
function onPlayerJoin ( source )
xmlLoadFile ("playerstats.xml")
outputChatBox("Loading Acc...", source )
if ( saveFile ) then
local playeraccount = getClientName ( source )
if ( playeraccount ) then
local subnode = xmlFindSubNode ( saveFile, "users", 0 )
local stats = xmlFindSubNode ( subnode, "player", statscheck )
local name = xmlNodeGetAttribute( stats, "name" )
if (( stats ) and (name ==""..playeraccount)) then
local playermoney = xmlNodeGetAttribute( stats, "money" )
local playerskin = xmlNodeGetAttribute( stats, "skin")
if (( playermoney ) and ( playerskin )) then
setPlayerMoney ( source, playermoney )
setPlayerSkin ( source, playerskin )
 
end
else
		    statscheck = statscheck +1
 
end
else
local stats = xmlCreateSubNode( subnode, "player" )
local name = xmlNodeSetAttribute( stats, "name", playeraccount )
xmlSaveFile ( saveFile )
end
else
outputChatBox("File Not Found", source )	
end
end
addCommandHandler("loadaccount1", onPlayerJoin )
 
addCommandHandler ( "saveaccount1", onPlayerQuit )  
function stoploading ()
xmlUnloadFile( saveFile )
end
addEventHandler("onResourceStop", getResourceRootElement(getThisResource()), stoploading )

and heres the .xml that the lua should find from

<playerstats>
  <users>
     <player name="Dudeinblack" money="99999999" skin="1337"/>
     <player name="ArmedGuy-JNRNS" money="100000" skin="197"/>
     <player name="Armadillo" money="1" skin="13" />
  </users>
</playerstats>

Like for example, I Want The .lua to search the xml above for an Subnode with the attribute name="ArmedGuy-JNRNS"... and what i know, to do that the script gotta loop through every singe subnode named player until it finds one with the Attribute name="ArmedGuy-JNRNS"...

I Would Like to get some help with this as i only need this working for my scripts to make them tun propertly

Ty

Link to comment

First thing I wanted to say is that what you're trying to do is not safe, you can join with someone else's name and mess up his stats... They should login into their accounts. Use Account functions for it instead (if and ONLY if you're using 1.0 nightly builds, if you're using DP2.x don't use Account functions).

This is an example how to loop through xml file:

...
local rootNode = xmlLoadFile( "playerstats.xml" );
local usersNode = xmlFindSubNode( rootNode, "users", 0 );
local usersCount = 0;
local playerNode = xmlFindSubNode( usersNode, "player", usersCount );
while playerNode do
local name = xmlNodeGetAttribute( playerNode, "name" );
if ( name == "ArmedGuy-JNRNS" ) then
        outputChatBox( "user 'ArmedGuy-JNRS' found in xml file" );
end
   usersCount = usersCount + 1;
   playerNode = xmlFindSubNode( usersNode, "player", usersCount );
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...