Jump to content


Photo

Simplxml 1.0.2 Xml Parser


  • Please log in to reply
28 replies to this topic

#21 Ghostkeeper

Ghostkeeper

    That's Logic

  • New Member
  • 189 posts

Posted 08 January 2007 - 05:09 PM

Sorry for the slow reply. I was on a winter holliday to swiss. :(
As for your data, you are reading it all wrong. This code
area_name=xml_findvalue(XML_FILE_NAME,"Areas\ID\2\Name");
will look for the (root) map 'Areas', then search for 'ID', which it won't find in the root map (it's in the submap 'Area'), then look for '2', then 'Name'. What it SHOULD do, is look for the (root) map 'Areas', then search for 'Area', then for 'ID', and it's value. Then with the value stored, go back to the submap 'Area', and look for 'Name'. Then all that's left to do is store the Name with the appropriate ID.
Sounds complicated huh?

I hope this code will explain it to you:
pos = 0;
while(pos != -1)
    {
    ID = real(xml_findvalue(XML_FILE_NAME,"Areas/Area/ID",pos));
    Name = xml_findvalue(XML_FILE_NAME,"Areas/Area/Name",pos);
    area_names[ID] = Name;
    pos = xml_findposition(XML_FILE_NAME,"Areas/Area",pos);
    if(pos != -1)
        {
        pos += 9; //Skip to the next area. (every area has 9 items in your example, the map 'Area', four submaps and four items, one in each submap)
        };
    };
show_message("Name no. 1 is '"+area_names[1]+"'.#Name no. 2 is '"+area_names[2]+"'.");
While writing this piece of code to Ankerheegaard, I noticed that the two xml_find scripts threw an error because it compared a real value with a string sometimes. I fixed it in version 1.0.1. As for you, Ankerheegard, you don't have to download and implent it again, just place the string() function around the two values that are compared in the rules that throw an error.

Edited by Ghostkeeper, 08 January 2007 - 05:20 PM.

  • 0

#22 Carlos_Ramos

Carlos_Ramos

    GMC Member

  • New Member
  • 42 posts

Posted 01 February 2007 - 03:22 AM

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.

  • 0

#23 Petah

Petah

    GMC Member

  • GMC Member
  • 72 posts

Posted 28 August 2007 - 12:22 PM

In this XML code:
<root>
	<child>1</child>
	<child>2</child>
	<child>3</child>
</root>
How do I return the data from that second and third child tags?

As far as I can tell its not working like it should, I have tried this:
xid = xml_open("test.xml")

pos = 0
show_message(string(pos) + ":" + xml_findvalue(xid,"root\child",pos))
pos = xml_findposition(xid,"root\child",pos)
show_message(string(pos) + ":" + xml_findvalue(xid,"root\child",pos))
pos = xml_findposition(xid,"root\child",pos)
show_message(string(pos) + ":" + xml_findvalue(xid,"root\child",pos))

xml_close(xid)
Which results in 0:1,1:1,-1:1.
  • 0

#24 dahandy

dahandy

    GMC Member

  • New Member
  • 29 posts

Posted 20 November 2007 - 07:18 PM

What about this XML file? It returns -1 when I search for anything that is is "name" -section.

<?xml version="1.0" encoding="utf-8"?>

<userconfig>
	<var name="Language" value=""/>
	<var name="Theme" value="Classic"/>
	<var name="DisplayWidth" value="1440"/>
	<var name="DisplayHeight" value="900"/>
	<var name="DisplayBPP" value="32"/>
	<var name="DisplayWindowed" value="false"/>
	<var name="MenuBackgroundGraphics" value="High"/>
	<var name="GameGraphics" value="High"/>
	<var name="DrawLib" value="OPENGL"/>
	<var name="AudioEnable" value="true"/>
	<var name="AudioSampleRate" value="22050"/>
	<var name="AudioSampleBits" value="16"/>
	<var name="AudioChannels" value="Mono"/>
	<var name="EngineSoundEnable" value="true"/>
	<var name="ControllerMode1" value="Keyboard"/>
	<var name="KeyDrive1" value="Up"/>
	<var name="KeyBrake1" value="Down"/>
	<var name="KeyFlipLeft1" value="Left"/>
	<var name="KeyFlipRight1" value="Right"/>
	<var name="KeyChangeDir1" value="Space"/>
	<var name="ControllerMode2" value="Keyboard"/>
	<var name="KeyDrive2" value="A"/>
	<var name="KeyBrake2" value="Q"/>
	<var name="KeyFlipLeft2" value="Z"/>
	<var name="KeyFlipRight2" value="E"/>
	<var name="KeyChangeDir2" value="W"/>
	<var name="ControllerMode3" value="Keyboard"/>
	<var name="KeyDrive3" value="R"/>
	<var name="KeyBrake3" value="F"/>
	<var name="KeyFlipLeft3" value="T"/>
	<var name="KeyFlipRight3" value="Y"/>
	<var name="KeyChangeDir3" value="V"/>
	<var name="ControllerMode4" value="Keyboard"/>
	<var name="KeyDrive4" value="Y"/>
	<var name="KeyBrake4" value="H"/>
	<var name="KeyFlipLeft4" value="U"/>
	<var name="KeyFlipRight4" value="I"/>
	<var name="KeyChangeDir4" value="N"/>
	<var name="AutosaveHighscoreReplays" value="true"/>
	<var name="JoyIdx1" value="0"/>
	<var name="JoyAxisPrim1" value="1"/>
	<var name="JoyAxisPrimMax1" value="32760"/>
	<var name="JoyAxisPrimMin1" value="-32760"/>
	<var name="JoyAxisPrimUL1" value="1024"/>
	<var name="JoyAxisPrimLL1" value="-1024"/>
	<var name="JoyAxisSec1" value="0"/>
	<var name="JoyAxisSecMax1" value="32760"/>
	<var name="JoyAxisSecMin1" value="-32760"/>
	<var name="JoyAxisSecUL1" value="1024"/>
	<var name="JoyAxisSecLL1" value="-1024"/>
	<var name="JoyButtonChangeDir1" value="0"/>
	<var name="DefaultProfile" value="DaHandy"/>
	<var name="ScreenshotFormat" value="png"/>
	<var name="NotifyAtInit" value="false"/>
	<var name="ShowMiniMap" value="true"/>
	<var name="ShowEngineCounter" value="false"/>
	<var name="StoreReplays" value="true"/>
	<var name="ReplayFrameRate" value="25"/>
	<var name="CompressReplays" value="true"/>
	<var name="ContextHelp" value="true"/>
	<var name="MenuMusic" value="true"/>
	<var name="InitZoom" value="true"/>
	<var name="DeathAnim" value="true"/>
	<var name="WebHighscores" value="true"/>
	<var name="CheckHighscoresAtStartup" value="true"/>
	<var name="CheckNewLevelsAtStartup" value="true"/>
	<var name="ShowInGameWorldRecord" value="false"/>
	<var name="WebConfAtInit" value="false"/>
	<var name="WebHighscoresURL" value="http://xmoto.tuxfamily.org/highscores.xml"/>
	<var name="WebLevelsURL" value="http://xmoto.tuxfamily.org/levels.xml"/>
	<var name="WebThemesURL" value="http://xmoto.tuxfamily.org/themes.xml"/>
	<var name="WebThemesURLBase" value="http://xmoto.tuxfamily.org/sprites"/>
	<var name="WebRoomsURL" value="http://xmoto.tuxfamily.org/rooms.xml"/>
	<var name="WebHighscoresIdRoom" value="1"/>
	<var name="ProxyType" value=""/>
	<var name="ProxyServer" value=""/>
	<var name="ProxyPort" value="-1"/>
	<var name="ProxyAuthUser" value=""/>
	<var name="ProxyAuthPwd" value=""/>
	<var name="WebHighscoreUploadURL" value="http://xmoto.tuxfamily.org/tools/UploadReplay.php"/>
	<var name="WebHighscoreUploadLogin" value=""/>
	<var name="WebHighscoreUploadPassword" value=""/>
	<var name="EnableGhost" value="true"/>
	<var name="GhostStrategy_MYBEST" value="true"/>
	<var name="GhostStrategy_THEBEST" value="true"/>
	<var name="GhostStrategy_BESTOFROOM" value="true"/>
	<var name="ShowGhostTimeDiff" value="true"/>
	<var name="DisplayGhostInfo" value="true"/>
	<var name="HideGhosts" value="false"/>
	<var name="GhostMotionBlur" value="false"/>
	<var name="QSQualityMIN" value="1"/>
	<var name="QSDifficultyMIN" value="1"/>
	<var name="QSQualityMAX" value="5"/>
	<var name="QSDifficultyMAX" value="1"/>
	<var name="MultiStopWhenOneFinishes" value="true"/>
</userconfig>

Edited by dahandy, 20 November 2007 - 07:19 PM.

  • 0

#25 kiujhytg2

kiujhytg2

    GMC Member

  • New Member
  • 2 posts

Posted 02 November 2009 - 04:04 PM

[quote name='Petah' post='2303857' date='Aug 28 2007, 01:25 PM']In this XML code:
xid = xml_open(get_open_filename("XML files|*.xml",""))pos = xml_findposition(xid,"root\child",0)  //Find the position of the 1st tag called "child" in the tag "root"show_message(string(pos) + ":" + xml_findvalue(xid,"child",pos)) //Give the value of the 1st tag called "child". Note that the path is "child" not "root\child" because we are already inside the tag "root"pos = xml_findposition(xid,"child",pos+1) //Find the position of the next tag called "child". Again, we are already in "root" so we don't need to include it in the path. We start a pos+1 to avoid repeating tagsshow_message(string(pos) + ":" + xml_findvalue(xid,"child",pos)) //Give the value of the next tag called "child"pos = xml_findposition(xid,"child",pos+1) //And again, find the position of the next tag called "child"show_message(string(pos) + ":" + xml_findvalue(xid,"child",pos)) //And again, give the valuexml_close(xid)

Summary of changes:
The 1st value of pos is xml_findposition(xid,"root\child",0) not 0
xml_findvalue and xml_findposition (except the 1st xml_findposition) is given path "child" not "root\child"
xml_findposition is given start position pos+1 not pos

Hope this helps.
  • 0

#26 DefuzionGames

DefuzionGames

    GMC Member

  • GMC Member
  • 1417 posts
  • Version:Unknown

Posted 15 March 2012 - 02:04 PM

link is down
  • 0

#27 Fraot

Fraot

    GMC Member

  • GMC Member
  • 11 posts

Posted 09 May 2012 - 05:54 PM

Hey! A beautiful idea!

I want to use xml's in my game for ease of translation in the future, but for now, I want to easily edit all the texts in my game.

How to call a certain text structure?

Let's say I have this little code:
<dialogs>
    <bosses>
        <boss1>
             <d1>
              "Hey dude! Lol."
             </d1>
             <d2>
               "Hahahahah, you suck."
             </d2>
        </boss1>
    </bosses>
</dialogs>

I know I would use "dialogs/bosses/boss1/d1" for the first line. But how to call it in a draw_text<more stuff>();?
  • 0

#28 grugin

grugin

    GMC Member

  • GMC Member
  • 116 posts

Posted 08 June 2012 - 09:20 AM

I can confirm that the link is down. Someone can re-upload it please ?
  • 0

#29 thecrazygamemaster

thecrazygamemaster

    Devcodier

  • GMC Member
  • 670 posts
  • Version:GM:Studio

Posted 22 July 2012 - 08:08 AM

Hey! A beautiful idea!

I want to use xml's in my game for ease of translation in the future, but for now, I want to easily edit all the texts in my game.

How to call a certain text structure?

Let's say I have this little code:

<dialogs>
    <bosses>
        <boss1>
             <d1>
              "Hey dude! Lol."
             </d1>
             <d2>
               "Hahahahah, you suck."
             </d2>
        </boss1>
    </bosses>
</dialogs>

I know I would use "dialogs/bosses/boss1/d1" for the first line. But how to call it in a draw_text<more stuff>();?


Load each element into a separate variable and draw both variables separately using draw_text(x,y,variablename). Link is down, put it up!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users