Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-25-2007, 05:24 PM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
XML -> Object

To go with the Object -> XML conversion I posted a while back: http://forums.graalonline.com/forums...ad.php?t=74337

PHP Code:
function readXML(filename,object) {
  
this.xml = new TStaticVar();
  
this.xml.loadlines(filename);
  
this.stack = {};
  while(
this.xml.size() > 0) {
    
temp.tag getTagName(this.xml[0]);
    if (
startTag(this.xml[0])) {
      
this.stack.insert(0,temp.tag);
      
//object.(@varStack()) = new TStaticVar();
      
varStack();
      if (
this.stack.size() == 1)this.roottag temp.tag;
    }
    else if (
endTag(this.xml[0])) {
      
this.stack.delete(0); //would be good here to check it's removing the correct end tag.
    
}
    else {
     if (
temp.tag == "index"
     
makevar("object."@varStack()).add(this.xml[0].substring(this.xml[0].pos(">")+1,(this.xml[0].pos("</"))-(this.xml[0].pos(">")+(">").length())));
     else {      
      
vs varStack();
      if (
vs == ""
        
object.(@temp.tag) = this.xml[0].substring(this.xml[0].pos("<"@temp.tag@">")+("<"@temp.tag@">").length(),(this.xml[0].pos("</"))-(this.xml[0].pos("<"@temp.tag@">")+("<"@temp.tag@">").length()));
        
      else  
        
makevar("object."@vs@"."@temp.tag) = this.xml[0].substring(this.xml[0].pos("<"@temp.tag@">")+("<"@temp.tag@">").length(),(this.xml[0].pos("</"))-(this.xml[0].pos("<"@temp.tag@">")+("<"@temp.tag@">").length()));
        
     }
      
    }
    
this.xml.delete(0);
  }
}

function 
varStack() {
 
temp.res "";
  for(
sthis.stack) {
    if (
!= this.roottag) {
     if (
temp.res == ""temp.res s;
     else 
temp.res "." temp.res;
    }
  }
  return 
temp.res;


function 
endTag(line) {
  
temp.tag getTagName(line);
  return 
temp.tag.starts("/");
}
function 
startTag(line) {
  
temp.tag getTagName(line);
  if (
temp.tag.starts("/")) return false;
  else if (
line.pos("<"@temp.tag@">") >= && line.pos("</"@temp.tag@">") == -1) return true;
}
function 
getTagName(line) {
if (
line.pos("<") >= && line.charat(line.pos("<")+1) != '?') {
      
temp.open line.pos("<");
      
temp.close line.pos(">");
      for (
temp.open<= temp.closei++) {
        if (
line.charat(i) == ' ') { temp.close i; break; }
      }
      if ((
temp.space temp.close) && (temp.space temp.open)) {
            
temp.close temp.space;
      }
      return 
line.substring(temp.open+1,temp.close-temp.open-1);
}

usage:
PHP Code:
readXML(xml fileobject);
//e.g.
readXML("JkWhoSaysNi.xml",findplayer("JkWhoSaysNi")); 
The object is sent to the function like that because for objects like player you can't do player = readXML(file) because player itself is not writable like that. It would be possible to have a more OO approach though using something like player.readXML(file); but do what you want with the code, it wouldn't be hard to change it.

There are some restrictions. It won't read just any XML file. The XML file has to:
Have start and end tags for objects on their own lines e.g.
PHP Code:
//wont work:
<player><weapons>...</weapons></player>

//will work:
<player>
<
weapons>
...
</
weapons>
</
player
It would be possible to remove this restriction but it would require looping through the characters on each line and it would be much less efficient. This script is only really designed to import XML files that have been created using my exporter anyway.

There is also no error checking. An invalid XML file will get parsed incorrectly and not produce the object you want.

Enjoy!
-Knight
__________________

Coming soon (Hopefully:P)

Last edited by JkWhoSaysNi; 06-26-2007 at 12:24 AM..
Reply With Quote
  #2  
Old 06-25-2007, 07:42 PM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
Ok, i thought it worked but it doesn't for some reason this wont work:

PHP Code:
"something.test";
object.(@r).foo "Test"
For some reason it thinks there are dots in the var name. so something.test is listed as a variable in getVarNames() of object.

Strange. Any ideas?
__________________

Coming soon (Hopefully:P)
Reply With Quote
  #3  
Old 06-25-2007, 07:52 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by JkWhoSaysNi View Post
Strange. Any ideas?
makevar.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #4  
Old 06-25-2007, 08:37 PM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
thanks i updated the script in the first post
__________________

Coming soon (Hopefully:P)
Reply With Quote
  #5  
Old 06-25-2007, 10:31 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by JkWhoSaysNi View Post
Ok, i thought it worked but it doesn't for some reason this wont work:

PHP Code:
"something.test";
object.(@r).foo "Test"
For some reason it thinks there are dots in the var name. so something.test is listed as a variable in getVarNames() of object.

Strange. Any ideas?

object.(@ r @ ".foo") = "Test";


Would work I think,
but I would say use makevar as Twinny stated
__________________
Reply With Quote
  #6  
Old 06-26-2007, 12:07 AM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
Yeah, i already fixed it

Here's an example:

Test.xml
PHP Code:
<?xml version="1.0">
<object>
    <stringvar>test</stringvar>
    <intvar>55</intvar>
    <objvar>
        <intvar1>12</intvar1>
        <intvar2>15</intvar2>
        <stringvar>test2</stringvar>
        <obj2>
            <testvar>foo</testvar>
            <arrayvar>
                <index id="1">3</index>
                <index id="2">9</index>
                <index id="3">1</index>
            </arrayvar>
        </obj2>
    </objvar>
</object>
example usage

PHP Code:
foo = new TStaticVar();
readXML("levels/test.xml",foo);
echo(
foo.objvar.obj2.testvar); //echos foo to RC. 
__________________

Coming soon (Hopefully:P)
Reply With Quote
  #7  
Old 06-26-2007, 02:19 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Maybe you could make the tag names customizable so one could shorten the file?
PHP Code:
tagname_object=obj
tagname_stringvar
=v1
tagname_intvar
=v0
tagname_objvar
=v2
tagname_arrayvar
=v3 
It would use those entries for the tag names then, do you understand?
__________________
Reply With Quote
  #8  
Old 06-26-2007, 02:24 AM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
it would be possible, you'd just need 2 arrays, one with names and one with shortened names then have the import/export function look up the shortened/full var name

Although it wont really make that much difference. For simplicities sake it's easier to use the graal var names as the tag names and the problem with that is that you can only import/export specific objects.

My XML file was just an example. You could make any XML file you wanted (provided you had the start and end tags on their own lines for objects) with the tags you wanted, of course the variable names when the file was imported would be the tag names from the file.
__________________

Coming soon (Hopefully:P)
Reply With Quote
  #9  
Old 07-04-2007, 04:04 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
What is the advantage of saving vars in XML versus just savevars and loadvars?
__________________
Reply With Quote
  #10  
Old 07-04-2007, 04:08 AM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
it will save/load any sub objects recursively.
__________________

Coming soon (Hopefully:P)
Reply With Quote
  #11  
Old 07-09-2007, 04:28 PM
zokemon zokemon is offline
That one guy...
zokemon's Avatar
Join Date: Mar 2001
Location: Sonoma County, California
Posts: 2,925
zokemon is a jewel in the roughzokemon is a jewel in the rough
Send a message via ICQ to zokemon Send a message via AIM to zokemon Send a message via MSN to zokemon Send a message via Yahoo to zokemon
I don't see why they need their own lines...
You could easily loop it all through a string.pos("<") check which isn't every character at all.
__________________
Do it with a DON!
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 02:06 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.