mIRC Homepage
Posted By: ots654685 Reading values from *.xml file - 02/01/08 02:41 PM
I have an *.xml (RushSite.xml) file here and i would like to retrieve specific values from it, site names to be more precise but after a lot of reading and searching i'm unable to read complete content of it.

My first attempt was this:
Code:
//echo $read(C:\FTPRush\RushSite.xml, w, *SITE NAME=*)
*Trying to get atleast some values from the file

Then i tried to add a while loop in it but i'm stuck in getting results back of it in active window.

Code:
alias Names {
  while ($exists(%FTPRushDir $+ RushSite.xml) == $false) { set %FTPRushDir $sdir(c:\,Select your FTPRush's folder)
  }
  var %nr = 1, %matches
  while ($read(%FTPRushDir $+ RushSite.xml, w, *SITE NAME*, %nr)) {
  var %matches = %matches $readn
  var %nr = $calc($readn +1)
  }
}
What i want to accomplish is to add those names in a hash table but before i go there i first wanna make this work.
Posted By: TropNul Re: Reading values from *.xml file - 03/01/08 04:20 PM
Can you please show us what the contents of the file looks like? That'll help a lot to think about adjusting your function or even recoding something else. Thank you.

Cordialement
Posted By: Thrull Re: Reading values from *.xml file - 04/01/08 09:40 AM
To my knowledge, $read will only work well with text files. It may or may not work with other file types, and the results probably wouldn't be all that useful.

I'd suggest using $fread instead. You may have the same issues, but you'd have an easier time cutting the excess data that a non-text file will have.
Posted By: ots654685 Re: Reading values from *.xml file - 05/01/08 10:10 PM
Originally Posted By: TropNul
Can you please show us what the contents of the file looks like? That'll help a lot to think about adjusting your function or even recoding something else. Thank you.

Cordialement

Sorry for late reply.
I pasted content here
http://pastebin.com/f7938799f

@Thrull,
Just reading the first line went well so far. But if $read isn't gonna work changing it to $fread isn't such a big problem i think
Posted By: ots654685 Re: Reading values from *.xml file - 08/01/08 02:00 PM
I have totally no clue how to start.
After doing some search on the forum because suggested to use fread i found this thread
https://forums.mirc.com/ubbthreads.php?ubb=showflat&Number=189162&page=6&PHPSESSID=af

Is this example gonna help me accomplish what i would like to have?

p.s.:
I changed it a bit but it didn't gave me the information i wanted, output was: SITE NAME <SITES> - 1 : - 21 :
Posted By: genius_at_work Re: Reading values from *.xml file - 08/01/08 03:26 PM
Try this:

Code:

alias xmltest {
  var %file = xmltest.xml
  filter -fk %file xmlhash *<site name=* uid=*>
}

alias xmlhash {
  var %r = /<site name="(.+?)" uid="([\da-f]+?)">/i
  if (!$regex($1,%r)) return
  echo -a $regml(2) : $regml(1)
}



/xmltest

-genius_at_work
Posted By: ots654685 Re: Reading values from *.xml file - 09/01/08 10:25 AM
Thanks, works perfect.
Didn't thought it was that simple (small code)
Going to figure out how it works later today.

Now i'm gonna fix it so it will be placed in hash table wink

Thanks again.
Posted By: ots654685 Re: Reading values from *.xml file - 09/01/08 02:15 PM
@genius_at_work
I rebuild your code a bit so it fits with the rest.

Now i was wondering if it really is needed to keep this format when adding data in hash tables?
/hadd <table> <data_name> <value>

I thought being smart and just replace echo with /hadd but got punished right after it because servers with spaces in there name are getting cut. Maybe someone knows a workaround for it?

Another thing i was wondering about what the best method was to see if site is active or not?
Is using sockets the best method for it or just try to login and if it time's out site is down?
Rush has a command to disable site and stores values itself so the script i'm trying to create doesn't need an extra value for active or not, maybe just for user to show but not really needed.

For now this is what i have. I don't think it can't be made any faster, just the space problem when adding data
Code:
alias serversadd {
  var %r = /<site name="(.+?)" uid="([\da-f]+?)">/i
  if (!$regex($1,%r)) return
  echo -a $regml(1)
  /hadd servers $regml(1)
}


Posted By: Mpdreamz Re: Reading values from *.xml file - 09/01/08 02:29 PM
syntax for /hadd is:
/hadd table key value

so:
/hadd servers $regml(1)
might not always yield the correct call. Like you indicated spaces will be interpreted as as a separation between key and value.

you could try something like:

/hadd servers $regml(2) $regml(1)

uid sounds unique and non spaced (then again that's just a guess!)

otherwise replace space within $regml(1) with something like "S1p1A1c1E" and $replacecs it back to a space when needed would be fail save enough.
Posted By: ots654685 Re: Reading values from *.xml file - 09/01/08 02:57 PM
Thanks for confirming it.
$regml(2) is an option as it indeed is unique.

But I'm thinking putting this on my low prio list as it seems to complicated to establish what i would like to have, at least it's above my knowledge.

I will explain.
With a simple command line it's possible to get FTPRush respond on just a site name and connect to it.
When site is not active anymore, and disabled, this method no longer works and it's not possible to see if site is active or not as you can't connect with site name only.
With that in mind i need to find something else to make it work
Posted By: ots654685 Re: Reading values from *.xml file - 17/03/08 01:09 PM
Started to pick this up again but it's just giving headache.
I thought with the above code it would be simple for me to get other items and link them to each other but i was wrong.

I wanted to get another item that isn't always available on all sections, example:
Quote:
<SITE NAME="Microsoft" UID="4860BC398AA249E89453B69089CCBAC5">
<HOST>
ftp.microsoft.com
</HOST>
<NF>
True
</NF>
<AUTOREF ENABLE="False" />
<STATIC TOTAL="0" LAT="0" UP="0" DN="0" FXP="0" FXPD="0" />
<DISABLE>
True
</DISABLE>
</SITE>
; NEW BLOCK BEGINS
<SITE NAME="Sun" UID="2F5AF8E019F64286A864168372528990">
<HOST>
ftp.sun.com
</HOST>
</SITE>

Above is a snippet from 2 blocks, the first one (Microsoft), is disabled which i can tell because of this part (enabled sections don't have it)
Quote:
<DISABLE>
True
</DISABLE>
So i thought extending code with a second item
Code:
alias xmltest {
  var %file = xmltest.xml
  filter -fk %file xmlhash *<site name=* uid=*>
  filter -fk %file xmlhash *<disable*
}

alias xmlhash {
  var %r = /<site name="(.+?)" uid="([\da-f]+?)">/i
  var %r2 = /<disable="(.+?)" /i
  if (!$regex($1,%r)) return
  if (!$regex($1,%r2)) return
  echo -a $regml(1)
}
But that was to easy for making it work so i did some reading but can't find proper documentation or examples on how to handle this.

Second thing i ran in to is sorting data, when i echo it to mirc it's sorted correctly but saving data to hash file for later use it's just random.
Posted By: ots654685 Re: Reading values from *.xml file - 01/04/08 04:18 PM
Since above idea isn't working ill do a final attempt for myself to get it working.

Is it possible to define blocks in script and that it reads everything as a block with start point <SITE and end point </SITE>
(maybe extend code with possible options between start and end point and create vars of them)

If i can read things as a block and loop it then i can process every block separate and achieve what i'm trying to do.
Posted By: argv0 Re: Reading values from *.xml file - 01/04/08 04:37 PM
Rather than trying to hack up a bunch of regexes when regexes simply don't work, write an xml parser. Or use one of the existing parsers:

http://www.mircscripts.org/archive.php?stype=all&squery=xml

You'll thank yourself later.
© mIRC Discussion Forums