I know it's late in the game, but I've updated the xml_findvalue() code. I think I might have messed up earlier, and certain elements could not be read. This script now fully loads a any element at any address, granted that the node exists. I also commented the code a little better.
Just copy and paste into the old xml_findvalue script.
//////xml_findvalue//////////////////////////////////////////////////////////
//This function looks for a tag with a specific pathname and returns the //
//first value within that tag. //
// //
//argument0: (real) ID of the XML file, as returned by xml_open(). //
//argument1: (string) Path of the tag to look for, submaps seperated by '/'//
// or by '\', and including the root map, but not the final slash. //
//argument2: (real) The place to start looking, the first matching tag is //
// returned. //
//return: (string) The first value within the tag with the given path. A //
// (real) value -1 is returned if the path could not be found. //
///////////////////////////////////////////////////////////Carlos Ramos//////
//Setup Variables
argument1 = string_replace(argument1,"/","\"); //Replace all / with \ in the original address
address[0] = argument1; //Stores each element in the address into its own variable (index 0 stores the original address)
element_num = 0; //Number of address elements
cur_val = ""; //The value at the current location within the XML File
cur_element = 1; //The address element we are looking for
cur_level = 0; //The level of the last address element we found
last_good_position = 1; //Stores the position in the XML file of the last good element
//Divide the address into separate elements
for(i = 0; i<= string_count("\",argument1);i += 1)
{
address[i+1] = string_copy(address[0],1,string_pos("\",address[0])-1); //Copy all characters to the left of the first \
if(address[i+1] == ""){address[i+1] = address[0];} //If no '\' are found, then copy the rest of the string
address[0] = string_copy(address[0],string_pos("\",address[0])+1,string_length(address[0])); //Remove all characters to the left of the first \ in the address[0] variable...truncuate address[0]
element_num += 1; //Increment the Element Number variable
}
//Search through the entire file
repeat(xml_getlength(argument0))
{
cur_value = xml_getvalue(argument0, argument2); //Get the value at the current location
//Compare the current value with the current Address Element
if(cur_value == address[cur_element])
{
//The level we are looking at is a grandchild, so we must start over
if(xml_getlevel(argument0, argument2) > xml_getlevel(argument0,last_good_position)+1)
{
cur_level = 0; //Clear the variable that tells us the location of a found element, because we no longer know
cur_element = 1; //Start from scratch
}
cur_level = xml_getlevel(argument0, argument2); //Remember this address elements level...when we goto a level that is lower (ie is either a parent of this element or not a family member) then stop looking through the children for the next element
last_good_position = argument2; //Remember the position of this address element
//If we are on the last address element, then we found what we were looking for!!!
if(cur_element >= element_num){return xml_getvalue(argument0,argument2+1);} //Return this elements Child Value
cur_element += 1; //We found an element, time to start looking for the next one
//If the next element is not a child, or is the EOF...then we must restart the search at the current position
if((xml_getlevel(argument0,argument2+1) <= cur_level) || argument2+1 > xml_getlength(argument0))
{
cur_level = 0; //Clear the variable that tells us the location of a found element, because we no longer know
cur_element = 1; //Start from scratch
}
}
argument2 += 1; //Look at the next element
}
return -1;So for example, taking a look at the following XML file:
<Lemmings>
<Walk>
<Sprite>Sprites\Lemming_Walk_01.bmp</Sprite>
<Frames>8</Frames>
<Speed>0.5</Speed>
<X_Origin>4</X_Origin>
<Y_Origin>6</Y_Origin>
</Walk>
<Fall>
<Sprite>Sprites\Lemming_Fall_01.bmp</Sprite>
<Frames>4</Frames>
<Speed>.25</Speed>
<X_Origin>4</X_Origin>
<Y_Origin>6</Y_Origin>
</Fall>
<Splat>
<Sprite>Sprites\Lemming_Splat_01.bmp</Sprite>
<Frames>17</Frames>
<Speed>.25</Speed>
<X_Origin>10</X_Origin>
<Y_Origin>6</Y_Origin>
</Splat>
<Dig>
<Sprite>Sprites\Lemming_Dig_01.bmp</Sprite>
<Frames>8</Frames>
<Speed>.25</Speed>
<X_Origin>7</X_Origin>
<Y_Origin>8</Y_Origin>
</Dig>
</Lemmings>
You could easily grab the sprite filename for say, a Digging Lemming, by the following code :
xml_findvalue(XMLID, "Lemmings\Dig\Sprite");
or
xml_findvalue(XMLID, "Dig\Sprite");
Edited by Carlos_Ramos, 01 February 2007 - 03:22 AM.