Author Topic: JSon to Rexx (stem)  (Read 15324 times)

Fr4nk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 32
    • View Profile
    • subsys.de
Re: JSon to Rexx (stem)
« Reply #15 on: January 08, 2014, 10:25:31 am »
Hiho,

well it is not a real XCenter Widget. I prefer to use vanilla wps-objects. But of course you might drop such an object on your x-center/warpcenter/bubblepad/whatever, get an icon and a fly-over forecast.

I'm not sure about IP localization, maybe we do it as an option - but also manually localization entries (of course). Btw a nice geo-localization service is http://en.utrace.de/api.php - they provide very simple requests and returns XML-based data to get latitude and longitude.

Hopefully I'll find free time for this next weekend.

cu shortly :)
Fr4nk

« Last Edit: January 08, 2014, 03:20:53 pm by Fr4nk »

Jan-Erik Lärka

  • Global Moderator
  • Full Member
  • *****
  • Thank You
  • -Given: 5
  • -Receive: 11
  • Posts: 151
    • View Profile
Re: JSon to Rexx (stem)
« Reply #16 on: January 08, 2014, 02:47:19 pm »
Ahh, I followed the link and like it alot! Really nice!

As a matter of fact, my initial post was aimed at you when I wrote it to use the openweather service.  ;)

//Jan-Erik

Below I've added selection 3 and 4 to the xml-parser if someone would need it.
Code: [Select]
parse_xml_tags: PROCEDURE /* xmlString, tag <<, tag_value>, selection> */
    prev_trace = TRACE( 'O' )
    PARSE UPPER ARG xmlString, tag, val, selection
    start_tag = '<'||tag
    INTERPRET "PARSE VALUE xmlString WITH pre'"||start_tag||val||"'extra'>'post"
    SELECT
        WHEN selection = 1 THEN /* tag */
        DO
            IF LENGTH( extra ) > 0 THEN
            DO
                PARSE VALUE SUBSTR( ARG(1), ABS( LENGTH( xmlString ) - LENGTH( post ) + 1 ) ) WITH pre'<'.
                RETURN pre
            END
        END
        WHEN selection = 2 THEN /* tag value */
            RETURN SUBSTR( ARG(1), ABS( LENGTH( xmlString ) - LENGTH( post ) - LENGTH( extra ) ), LENGTH( extra ) - ( RIGHT( extra, 1 ) = '/' ) )
        WHEN selection = 3 THEN /* tag value */
        DO
            tag_val = SUBSTR( ARG(1), ABS( LENGTH( xmlString ) - LENGTH( post ) - LENGTH( extra ) ), LENGTH( extra ) - ( RIGHT( extra, 1 ) = '/' ) )
            INTERPRET "PARSE CASELESS VALUE tag_val WITH .'"||val||'="'||"'ret_val'"||'"'||"'."
            IF LENGTH( ret_val ) > 0 THEN RETURN ret_val
            INTERPRET "PARSE CASELESS VALUE tag_val WITH .'"||val||"='ret_val ."
            IF LENGTH( ret_val ) > 0 THEN RETURN ret_val
            ELSE RETURN tag_val /* What 2 do else?! */
        END
        WHEN selection = 4 THEN /* tag name value */
        DO
            INTERPRET "PARSE VALUE xmlString WITH pre'"||start_tag||" NAME="||'"'||STRIP( val,, '"' )||'"'||" VALUE="||'"'||"'extra'"||'"'||" />'."
            IF LENGTH( STRIP( extra ) ) = 0 THEN
                INTERPRET "PARSE VALUE xmlString WITH pre'"||start_tag||" NAME="||STRIP( val,, '"' )||" VALUE="||"'extra'"||"/'."
            IF LENGTH( STRIP( extra ) ) = 0 THEN RETURN ''
            start_pos = POS( extra, TRANSLATE( xmlString ) )
            IF start_pos > 0 THEN RETURN SUBSTR( ARG(1), start_pos, LENGTH( extra ) )
            ELSE RETURN ''
        END
        WHEN RIGHT( extra, 1 ) <> '/' THEN
        DO
            end_tag = '</'||tag||'>'
            next = LENGTH( xmlString ) - LENGTH( post ) + 1
            open_tag = next
            INTERPRET "PARSE VALUE post WITH pre'"||end_tag||"'post"
            close_tag = LENGTH( xmlString ) - LENGTH( post ) - LENGTH( end_tag ) + 1
            count = COUNTSTR( start_tag||'>', xmlString, open_tag, close_tag ) + COUNTSTR( start_tag||' ', xmlString, open_tag, close_tag )
            DO WHILE count > 0
                next = close_tag + LENGTH( end_tag )
                close_tag = POS( end_tag, xmlString, next )
                IF close_tag = 0 THEN
                    close_tag = LENGTH( xmlString )
                post = SUBSTR( xmlString, next, close_tag - next )
               
                INTERPRET "PARSE VALUE post WITH .'"||start_tag||"'extra'/>'post"
               
                DO WHILE LENGTH( post ) > 0
                    next = close_tag - LENGTH( post )
                    INTERPRET "PARSE VALUE post WITH .'"||start_tag||"'extra'/>'post"
                END
                count = COUNTSTR( start_tag||'>', xmlString, open_tag, close_tag ) + COUNTSTR( start_tag||' ', xmlString, open_tag, close_tag ) - 1
            END
            IF close_tag = 0 THEN close_tag = LENGTH( ARG(1) ) - open_tag
            ELSE close_tag = close_tag - open_tag
            RETURN SUBSTR( ARG(1), open_tag, MAX( 1, close_tag ) )
        END
        OTHERWISE NOP
    END
    CALL TRACE prev_trace
RETURN ''

I've tested this code with regina that is somewhat limited when it come to PARSE, so you can try to replace:
INTERPRET "PARSE VALUE xmlString WITH pre'"||start_tag||" NAME="||'"'||STRIP( val,, '"' )||'"'||" VALUE="||'"'||"'extra'"||'"'||" />'."
with
val = STRIP( val,, '"' )
PARSE  VALUE xmlString WITH pre(start_tag)' NAME="'(val)'" VALUE="'extra'" />'
« Last Edit: January 08, 2014, 03:44:15 pm by Jan-Erik Lärka »

Fr4nk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 32
    • View Profile
    • subsys.de
Re: JSon to Rexx (stem)
« Reply #17 on: January 08, 2014, 03:22:56 pm »
Thank you - I'll check it out.