RetroBASIC

Basicprogramming(.org) => Code and examples => Topic started by: ScriptBasic on June 28, 2017, 05:52:28 PM

Title: XML Parser
Post by: ScriptBasic on June 28, 2017, 05:52:28 PM
It's been awhile since we had a code challenge here on the forum. I would like to propose a code challenge to parse XML content and extract the data. I've attached a G/L Accounts XML file that is the response from a query to a QuickBooks Online developer sandbox company.

The goal is to extract the <Id>, <Name>, <AccountType> and  <CurrentBalance> as a list.


Title: Re: XML Parser
Post by: ScriptBasic on June 29, 2017, 03:33:04 AM
As motivation, here is the parsed results in a Script BASIC application server web page.

This example actually gets the data from QuickBooks Online, extracts the data from the XML response and creates a HTML table from it.

QBO Account Listing (http://www.scriptbasic.org/home/qbo_account)

Title: Re: XML Parser
Post by: B+ on June 29, 2017, 05:10:44 AM
Code: [Select]
tload "acct_all.txt",all,2:dim f(3),a():f(0)="<Id>":f(1)="<Name>":f(2)="<AccountType>":f(3)="<CurrentBalance>":p=1:s=""
label loop:for i = 0 to 3 : ip = instr(p, all, f(i))
if ip then s += mid(all, ip, instr(ip + len(f(i)), all, "<") - ip) + "  " else tsave "acct_list.txt", a : end
p = instr(ip + len(f(i)) + 1, all, "<") : next : a << s + chr(13) : s = "" : goto loop

Code: [Select]
<Id>33  <Name>Accounts Payable (A/P)  <AccountType>Accounts Payable  <CurrentBalance>-1602.67 
<Id>84  <Name>Accounts Receivable (A/R)  <AccountType>Accounts Receivable  <CurrentBalance>5281.52 
<Id>7  <Name>Advertising  <AccountType>Expense  <CurrentBalance>0 
<Id>89  <Name>Arizona Dept. of Revenue Payable  <AccountType>Other Current Liability  <CurrentBalance>0 
<Id>55  <Name>Automobile  <AccountType>Expense  <CurrentBalance>0 
<Id>56  <Name>Fuel  <AccountType>Expense  <CurrentBalance>0 
<Id>8  <Name>Bank Charges  <AccountType>Expense  <CurrentBalance>0 
<Id>85  <Name>Billable Expense Income  <AccountType>Income  <CurrentBalance>0 
<Id>90  <Name>Board of Equalization Payable  <AccountType>Other Current Liability  <CurrentBalance>-370.94 
<Id>35  <Name>Checking  <AccountType>Bank  <CurrentBalance>1201.00 
<Id>9  <Name>Commissions &amp; fees  <AccountType>Expense  <CurrentBalance>0 
<Id>80  <Name>Cost of Goods Sold  <AccountType>Cost of Goods Sold  <CurrentBalance>0 
<Id>40  <Name>Depreciation  <AccountType>Other Expense  <CurrentBalance>0 
<Id>82  <Name>Design income  <AccountType>Income  <CurrentBalance>0 
<Id>86  <Name>Discounts given  <AccountType>Income  <CurrentBalance>0 
<Id>28  <Name>Disposal Fees  <AccountType>Expense  <CurrentBalance>0 
<Id>10  <Name>Dues &amp; Subscriptions  <AccountType>Expense  <CurrentBalance>0 
<Id>29  <Name>Equipment Rental  <AccountType>Expense  <CurrentBalance>0 
<Id>5  <Name>Fees Billed  <AccountType>Income  <CurrentBalance>0 
<Id>11  <Name>Insurance  <AccountType>Expense  <CurrentBalance>0 
<Id>57  <Name>Workers Compensation  <AccountType>Expense  <CurrentBalance>0 
<Id>25  <Name>Interest Earned  <AccountType>Other Income  <CurrentBalance>0 
<Id>81  <Name>Inventory Asset  <AccountType>Other Current Asset  <CurrentBalance>596.25 
<Id>58  <Name>Job Expenses  <AccountType>Expense  <CurrentBalance>0 
<Id>59  <Name>Cost of Labor  <AccountType>Expense  <CurrentBalance>0 
<Id>60  <Name>Installation  <AccountType>Expense  <CurrentBalance>0 
<Id>61  <Name>Maintenance and Repairs  <AccountType>Expense  <CurrentBalance>0 
<Id>62  <Name>Equipment Rental  <AccountType>Expense  <CurrentBalance>0 
<Id>63  <Name>Job Materials  <AccountType>Expense  <CurrentBalance>0 
<Id>64  <Name>Decks and Patios  <AccountType>Expense  <CurrentBalance>0 
<Id>65  <Name>Fountain and Garden Lighting  <AccountType>Expense  <CurrentBalance>0 
<Id>66  <Name>Plants and Soil  <AccountType>Expense  <CurrentBalance>0 
<Id>67  <Name>Sprinklers and Drip Systems  <AccountType>Expense  <CurrentBalance>0 
<Id>68  <Name>Permits  <AccountType>Expense  <CurrentBalance>0 
<Id>45  <Name>Landscaping Services  <AccountType>Income  <CurrentBalance>0 
<Id>46  <Name>Job Materials  <AccountType>Income  <CurrentBalance>0 
<Id>47  <Name>Decks and Patios  <AccountType>Income  <CurrentBalance>0 
<Id>48  <Name>Fountains and Garden Lighting  <AccountType>Income  <CurrentBalance>0 
<Id>49  <Name>Plants and Soil  <AccountType>Income  <CurrentBalance>0 
<Id>50  <Name>Sprinklers and Drip Systems  <AccountType>Income  <CurrentBalance>0 
<Id>51  <Name>Labor  <AccountType>Income  <CurrentBalance>0 
<Id>52  <Name>Installation  <AccountType>Income  <CurrentBalance>0 
<Id>53  <Name>Maintenance and Repair  <AccountType>Income  <CurrentBalance>0 
<Id>12  <Name>Legal &amp; Professional Fees  <AccountType>Expense  <CurrentBalance>0 
<Id>69  <Name>Accounting  <AccountType>Expense  <CurrentBalance>0 
<Id>70  <Name>Bookkeeper  <AccountType>Expense  <CurrentBalance>0 
<Id>71  <Name>Lawyer  <AccountType>Expense  <CurrentBalance>0 
<Id>43  <Name>Loan Payable  <AccountType>Other Current Liability  <CurrentBalance>-4000.00 
<Id>72  <Name>Maintenance and Repair  <AccountType>Expense  <CurrentBalance>0 
<Id>73  <Name>Building Repairs  <AccountType>Expense  <CurrentBalance>0 
<Id>74  <Name>Computer Repairs  <AccountType>Expense  <CurrentBalance>0 
<Id>75  <Name>Equipment Repairs  <AccountType>Expense  <CurrentBalance>0 
<Id>41  <Name>Mastercard  <AccountType>Credit Card  <CurrentBalance>-157.72 
<Id>13  <Name>Meals and Entertainment  <AccountType>Expense  <CurrentBalance>0 
<Id>14  <Name>Miscellaneous  <AccountType>Other Expense  <CurrentBalance>0 
<Id>91  <Name>MyClients  <AccountType>Accounts Receivable  <CurrentBalance>0 
<Id>44  <Name>Notes Payable  <AccountType>Long Term Liability  <CurrentBalance>-25000.00 
<Id>15  <Name>Office Expenses  <AccountType>Expense  <CurrentBalance>0 
<Id>34  <Name>Opening Balance Equity  <AccountType>Equity  <CurrentBalance>9337.50 
<Id>83  <Name>Other Income  <AccountType>Income  <CurrentBalance>0 
<Id>26  <Name>Other Portfolio Income  <AccountType>Other Income  <CurrentBalance>0 
<Id>27  <Name>Penalties &amp; Settlements  <AccountType>Other Expense  <CurrentBalance>0 
<Id>54  <Name>Pest Control Services  <AccountType>Income  <CurrentBalance>0 
<Id>3  <Name>Prepaid Expenses  <AccountType>Other Current Asset  <CurrentBalance>0 
<Id>16  <Name>Promotional  <AccountType>Expense  <CurrentBalance>0 
<Id>78  <Name>Purchases  <AccountType>Expense  <CurrentBalance>0 
<Id>6  <Name>Refunds-Allowances  <AccountType>Income  <CurrentBalance>0 
<Id>17  <Name>Rent or Lease  <AccountType>Expense  <CurrentBalance>0 
<Id>2  <Name>Retained Earnings  <AccountType>Equity  <CurrentBalance>0 
<Id>79  <Name>Sales of Product Income  <AccountType>Income  <CurrentBalance>0 
<Id>36  <Name>Savings  <AccountType>Bank  <CurrentBalance>800.00 
<Id>1  <Name>Services  <AccountType>Income  <CurrentBalance>0 
<Id>19  <Name>Stationery &amp; Printing  <AccountType>Expense  <CurrentBalance>0 
<Id>20  <Name>Supplies  <AccountType>Expense  <CurrentBalance>0 
<Id>21  <Name>Taxes &amp; Licenses  <AccountType>Expense  <CurrentBalance>0 
<Id>22  <Name>Travel  <AccountType>Expense  <CurrentBalance>0 
<Id>23  <Name>Travel Meals  <AccountType>Expense  <CurrentBalance>0 
<Id>37  <Name>Truck  <AccountType>Fixed Asset  <CurrentBalance>0 
<Id>39  <Name>Depreciation  <AccountType>Fixed Asset  <CurrentBalance>0 
<Id>38  <Name>Original Cost  <AccountType>Fixed Asset  <CurrentBalance>13495.00 
<Id>88  <Name>Unapplied Cash Bill Payment Expense  <AccountType>Expense  <CurrentBalance>0 
<Id>87  <Name>Unapplied Cash Payment Income  <AccountType>Income  <CurrentBalance>0 
<Id>32  <Name>Uncategorized Asset  <AccountType>Other Current Asset  <CurrentBalance>0 
<Id>31  <Name>Uncategorized Expense  <AccountType>Expense  <CurrentBalance>0 
<Id>30  <Name>Uncategorized Income  <AccountType>Income  <CurrentBalance>0 
<Id>4  <Name>Undeposited Funds  <AccountType>Other Current Asset  <CurrentBalance>2062.52 
<Id>24  <Name>Utilities  <AccountType>Expense  <CurrentBalance>0 
<Id>76  <Name>Gas and Electric  <AccountType>Expense  <CurrentBalance>0 
<Id>77  <Name>Telephone  <AccountType>Expense  <CurrentBalance>0 
<Id>42  <Name>Visa  <AccountType>Credit Card  <CurrentBalance>0 
Title: Re: XML Parser
Post by: B+ on June 29, 2017, 05:41:38 AM
Without tags:
Code: [Select]
tload "acct_all.txt",all,2:dim f(3),a():f(0)="<Id>":f(1)="<Name>":f(2)="<AccountType>":f(3)="<CurrentBalance>":p=1:s=""
label loop:for i = 0 to 3 : ip = instr(p, all, f(i))
if ip then s+=left(mid(all,ip+len(f(i)),instr(ip+len(f(i)),all,"<")-(ip+len(f(i))))+space(40),40) else tsave "lst.rtf",a:end
p = instr(ip + len(f(i)) + 1, all, "<") : next : a << s + chr(13) : s = "" : goto loop

Code: [Select]
33                                      Accounts Payable (A/P)                  Accounts Payable                        -1602.67                               
84                                      Accounts Receivable (A/R)               Accounts Receivable                     5281.52                                 
7                                       Advertising                             Expense                                 0                                       
89                                      Arizona Dept. of Revenue Payable        Other Current Liability                 0                                       
55                                      Automobile                              Expense                                 0                                       
56                                      Fuel                                    Expense                                 0                                       
8                                       Bank Charges                            Expense                                 0                                       
85                                      Billable Expense Income                 Income                                  0                                       
90                                      Board of Equalization Payable           Other Current Liability                 -370.94                                 
35                                      Checking                                Bank                                    1201.00                                 
9                                       Commissions &amp; fees                  Expense                                 0                                       
80                                      Cost of Goods Sold                      Cost of Goods Sold                      0                                       
40                                      Depreciation                            Other Expense                           0                                       
82                                      Design income                           Income                                  0                                       
86                                      Discounts given                         Income                                  0                                       
28                                      Disposal Fees                           Expense                                 0                                       
10                                      Dues &amp; Subscriptions                Expense                                 0                                       
29                                      Equipment Rental                        Expense                                 0                                       
5                                       Fees Billed                             Income                                  0                                       
11                                      Insurance                               Expense                                 0                                       
57                                      Workers Compensation                    Expense                                 0                                       
25                                      Interest Earned                         Other Income                            0                                       
81                                      Inventory Asset                         Other Current Asset                     596.25                                 
58                                      Job Expenses                            Expense                                 0                                       
59                                      Cost of Labor                           Expense                                 0                                       
60                                      Installation                            Expense                                 0                                       
61                                      Maintenance and Repairs                 Expense                                 0                                       
62                                      Equipment Rental                        Expense                                 0                                       
63                                      Job Materials                           Expense                                 0                                       
64                                      Decks and Patios                        Expense                                 0                                       
65                                      Fountain and Garden Lighting            Expense                                 0                                       
66                                      Plants and Soil                         Expense                                 0                                       
67                                      Sprinklers and Drip Systems             Expense                                 0                                       
68                                      Permits                                 Expense                                 0                                       
45                                      Landscaping Services                    Income                                  0                                       
46                                      Job Materials                           Income                                  0                                       
47                                      Decks and Patios                        Income                                  0                                       
48                                      Fountains and Garden Lighting           Income                                  0                                       
49                                      Plants and Soil                         Income                                  0                                       
50                                      Sprinklers and Drip Systems             Income                                  0                                       
51                                      Labor                                   Income                                  0                                       
52                                      Installation                            Income                                  0                                       
53                                      Maintenance and Repair                  Income                                  0                                       
12                                      Legal &amp; Professional Fees           Expense                                 0                                       
69                                      Accounting                              Expense                                 0                                       
70                                      Bookkeeper                              Expense                                 0                                       
71                                      Lawyer                                  Expense                                 0                                       
43                                      Loan Payable                            Other Current Liability                 -4000.00                               
72                                      Maintenance and Repair                  Expense                                 0                                       
73                                      Building Repairs                        Expense                                 0                                       
74                                      Computer Repairs                        Expense                                 0                                       
75                                      Equipment Repairs                       Expense                                 0                                       
41                                      Mastercard                              Credit Card                             -157.72                                 
13                                      Meals and Entertainment                 Expense                                 0                                       
14                                      Miscellaneous                           Other Expense                           0                                       
91                                      MyClients                               Accounts Receivable                     0                                       
44                                      Notes Payable                           Long Term Liability                     -25000.00                               
15                                      Office Expenses                         Expense                                 0                                       
34                                      Opening Balance Equity                  Equity                                  9337.50                                 
83                                      Other Income                            Income                                  0                                       
26                                      Other Portfolio Income                  Other Income                            0                                       
27                                      Penalties &amp; Settlements             Other Expense                           0                                       
54                                      Pest Control Services                   Income                                  0                                       
3                                       Prepaid Expenses                        Other Current Asset                     0                                       
16                                      Promotional                             Expense                                 0                                       
78                                      Purchases                               Expense                                 0                                       
6                                       Refunds-Allowances                      Income                                  0                                       
17                                      Rent or Lease                           Expense                                 0                                       
2                                       Retained Earnings                       Equity                                  0                                       
79                                      Sales of Product Income                 Income                                  0                                       
36                                      Savings                                 Bank                                    800.00                                 
1                                       Services                                Income                                  0                                       
19                                      Stationery &amp; Printing               Expense                                 0                                       
20                                      Supplies                                Expense                                 0                                       
21                                      Taxes &amp; Licenses                    Expense                                 0                                       
22                                      Travel                                  Expense                                 0                                       
23                                      Travel Meals                            Expense                                 0                                       
37                                      Truck                                   Fixed Asset                             0                                       
39                                      Depreciation                            Fixed Asset                             0                                       
38                                      Original Cost                           Fixed Asset                             13495.00                               
88                                      Unapplied Cash Bill Payment Expense     Expense                                 0                                       
87                                      Unapplied Cash Payment Income           Income                                  0                                       
32                                      Uncategorized Asset                     Other Current Asset                     0                                       
31                                      Uncategorized Expense                   Expense                                 0                                       
30                                      Uncategorized Income                    Income                                  0                                       
4                                       Undeposited Funds                       Other Current Asset                     2062.52                                 
24                                      Utilities                               Expense                                 0                                       
76                                      Gas and Electric                        Expense                                 0                                       
77                                      Telephone                               Expense                                 0                                       
42                                      Visa                                    Credit Card                             0                                       
                           
Title: Re: XML Parser
Post by: ScriptBasic on June 29, 2017, 05:41:46 AM
B+,

Here is my Script BASIC console mode version.

Code: [Select]
' QBO Account Table Listing

OPEN "acct_all.txt" FOR INPUT AS 1
qboxml = INPUT(LOF(1),1)

SPLITA qboxml BY "</Account>" TO accts

FOR x = 0 TO UBOUND(accts)
  IF  accts[x] LIKE "*<Id>*</Id>*<Name>*</Name>*<AccountType>*</AccountType>*<CurrentBalance>*</CurrentBalance>*" THEN
    PRINT joker(2),STRING(7-LEN(joker(2))," ")
    PRINT joker(4),STRING(40-LEN(joker(4))," ")
    PRINT joker(6),STRING(25-LEN(joker(6))," ")
    PRINT FORMAT("%~###,###.00-~",joker(8)),"\n"
  END IF
NEXT


jrs@jrs-laptop:~/work/Finch/sb/test$ time scriba qbo_acct.sb
33     Accounts Payable (A/P)                  Accounts Payable           1,602.67-       
84     Accounts Receivable (A/R)               Accounts Receivable        5,281.52         
7      Advertising                             Expense                        0.00               
89     Arizona Dept. of Revenue Payable        Other Current Liability        0.00               
55     Automobile                              Expense                        0.00               
56     Fuel                                    Expense                        0.00               
8      Bank Charges                            Expense                        0.00               
85     Billable Expense Income                 Income                         0.00               
90     Board of Equalization Payable           Other Current Liability      370.94-       
35     Checking                                Bank                       1,201.00         
9      Commissions &amp; fees                  Expense                        0.00               
80     Cost of Goods Sold                      Cost of Goods Sold             0.00               
40     Depreciation                            Other Expense                  0.00               
82     Design income                           Income                         0.00               
86     Discounts given                         Income                         0.00               
28     Disposal Fees                           Expense                        0.00               
10     Dues &amp; Subscriptions                Expense                        0.00               
29     Equipment Rental                        Expense                        0.00               
5      Fees Billed                             Income                         0.00               
11     Insurance                               Expense                        0.00               
57     Workers Compensation                    Expense                        0.00               
25     Interest Earned                         Other Income                   0.00               
81     Inventory Asset                         Other Current Asset          596.25         
58     Job Expenses                            Expense                        0.00               
59     Cost of Labor                           Expense                        0.00               
60     Installation                            Expense                        0.00               
61     Maintenance and Repairs                 Expense                        0.00               
62     Equipment Rental                        Expense                        0.00               
63     Job Materials                           Expense                        0.00               
64     Decks and Patios                        Expense                        0.00               
65     Fountain and Garden Lighting            Expense                        0.00               
66     Plants and Soil                         Expense                        0.00               
67     Sprinklers and Drip Systems             Expense                        0.00               
68     Permits                                 Expense                        0.00               
45     Landscaping Services                    Income                         0.00               
46     Job Materials                           Income                         0.00               
47     Decks and Patios                        Income                         0.00               
48     Fountains and Garden Lighting           Income                         0.00               
49     Plants and Soil                         Income                         0.00               
50     Sprinklers and Drip Systems             Income                         0.00               
51     Labor                                   Income                         0.00               
52     Installation                            Income                         0.00               
53     Maintenance and Repair                  Income                         0.00               
12     Legal &amp; Professional Fees           Expense                        0.00               
69     Accounting                              Expense                        0.00               
70     Bookkeeper                              Expense                        0.00               
71     Lawyer                                  Expense                        0.00               
43     Loan Payable                            Other Current Liability    4,000.00-       
72     Maintenance and Repair                  Expense                        0.00               
73     Building Repairs                        Expense                        0.00               
74     Computer Repairs                        Expense                        0.00               
75     Equipment Repairs                       Expense                        0.00               
41     Mastercard                              Credit Card                  157.72-       
13     Meals and Entertainment                 Expense                        0.00               
14     Miscellaneous                           Other Expense                  0.00               
91     MyClients                               Accounts Receivable            0.00               
44     Notes Payable                           Long Term Liability       25,000.00-     
15     Office Expenses                         Expense                        0.00               
34     Opening Balance Equity                  Equity                     9,337.50         
83     Other Income                            Income                         0.00               
26     Other Portfolio Income                  Other Income                   0.00               
27     Penalties &amp; Settlements             Other Expense                  0.00               
54     Pest Control Services                   Income                         0.00               
3      Prepaid Expenses                        Other Current Asset            0.00               
16     Promotional                             Expense                        0.00               
78     Purchases                               Expense                        0.00               
6      Refunds-Allowances                      Income                         0.00               
17     Rent or Lease                           Expense                        0.00               
2      Retained Earnings                       Equity                         0.00               
79     Sales of Product Income                 Income                         0.00               
36     Savings                                 Bank                         800.00         
1      Services                                Income                         0.00               
19     Stationery &amp; Printing               Expense                        0.00               
20     Supplies                                Expense                        0.00               
21     Taxes &amp; Licenses                    Expense                        0.00               
22     Travel                                  Expense                        0.00               
23     Travel Meals                            Expense                        0.00               
37     Truck                                   Fixed Asset                    0.00               
39     Depreciation                            Fixed Asset                    0.00               
38     Original Cost                           Fixed Asset               13,495.00       
88     Unapplied Cash Bill Payment Expense     Expense                        0.00               
87     Unapplied Cash Payment Income           Income                         0.00               
32     Uncategorized Asset                     Other Current Asset            0.00               
31     Uncategorized Expense                   Expense                        0.00               
30     Uncategorized Income                    Income                         0.00               
4      Undeposited Funds                       Other Current Asset        2,062.52         
24     Utilities                               Expense                        0.00               
76     Gas and Electric                        Expense                        0.00               
77     Telephone                               Expense                        0.00               
42     Visa                                    Credit Card                    0.00               

real   0m0.022s
user   0m0.020s
sys   0m0.000s
jrs@jrs-laptop:~/work/Finch/sb/test$

Title: Re: XML Parser
Post by: B+ on June 29, 2017, 05:48:50 AM
Hi John,

I like your format better!

Oh, LIKE, yes good.
Title: Re: XML Parser
Post by: ScriptBasic on July 02, 2017, 02:43:45 AM
I guess the days of code challenges are gone. Thanks B+ for your efforts.

Title: Re: XML Parser
Post by: B+ on July 02, 2017, 11:32:04 AM
Hey John,

After this challenge, I pulled out some code that was sitting on shelf for three months and finished it.
This challenge helped me clarify what I wanted that code to do.

So you never know what may become of an effort. ;)

And of course, I am always a fan of different ways to do something or how it might be done in different flavors or PLs.


Title: Re: XML Parser
Post by: ScriptBasic on July 02, 2017, 04:46:41 PM
Quote
So you never know what may become of an effort.

Good to hear. I feel my time invested wasn't a wasted effort.

Maybe we'll see a B+ LIKE function for SmallBASIC materialize.

Title: Re: XML Parser
Post by: B+ on July 02, 2017, 05:35:38 PM
 :) Yes, you might see a Python like dictionary SIM using the LIKE keyword.

Title: Re: XML Parser
Post by: ScriptBasic on July 02, 2017, 06:33:56 PM
I could never get my arms around regex as LIKE seems to handle most of my patten matching tasks.
Title: Re: XML Parser
Post by: ScriptBasic on July 03, 2017, 06:15:05 AM
Quote
Yes, you might see a Python like dictionary SIM using the LIKE keyword.

Can you expand on that?

File directory? What does Python bring to the table?
Title: Re: XML Parser
Post by: ScriptBasic on July 03, 2017, 06:45:12 AM
Maybe we should change direction with this thread and make the challenge writing a LIKE function in BASIC.  :D

Hopefully someone will come up with a better name than JOKER.

BTW The Script BASIC LIKE can be either case sensitive (default) or case insensitive.
Title: Re: XML Parser
Post by: B+ on July 03, 2017, 01:34:37 PM
Code: [Select]
' like test.bas SmallBASIC 0.12.9 (B+=MGA) 2017-07-03
dim hello(), world()
open "Word List.txt" for input as #1
while eof(1)=0
  lineinput #1, fline
  two = left(fline,2)
  if two <> lasttwo then
    locate 1, 1 : ? space(20) : locate 1, 1 : ? two : lasttwo = two
  fi
  if fline like "h*" then
    if fline like "h?ll?" then hello << fline : ? fline
  elif fline like "w*" then
    if fline like "w?r?d" then world << fline : ? fline
  fi
wend
close #1
cls

for h in hello
  for w in world
    ? h;" ";w,
    c++ : if c mod 6 = 0 then ?
  next
next
?
pause
Title: Re: XML Parser
Post by: B+ on July 03, 2017, 01:59:45 PM
Python dictionary structure is a variable/value pairing like what might be used for properties. As a common acquaintance of ours once explained it, instead of having an array with numeric indexes, you have an array of string indexes.

For BASIC sim of the dictionary structure, I would use strings and I would use one delimiter for the var/value pair, probably = sign, and another delimiter for the pairings probably a space.
Title: Re: XML Parser
Post by: ScriptBasic on July 04, 2017, 06:41:17 AM
That sounds like the Script BASIC HASH (http://www.scriptbasic.org/docs/hash/mod_hash_toc.html) extension module functionality.

Or you could use an associative array as a hash.

Code: [Select]
dict{"A"} = 1
dict{"B"} = "bee"
dict{"C"} = "sea"

FOR x = 0 TO UBOUND(dict) STEP 2
  PRINT "Key = ",dict[x],"  Value = ",dict[x+1],"\n"
NEXT

b = "B"
PRINT dict{b},"\n"


jrs@jrs-laptop:~/sb/examples/test$ scriba assray.sb
Key = A  Value = 1
Key = B  Value = bee
Key = C  Value = sea
bee
jrs@jrs-laptop:~/sb/examples/test$
Title: Re: XML Parser
Post by: B+ on July 04, 2017, 02:54:22 PM
:) Yes, that's the idea!

Can you reverse the process eg list all the keys that have the value "bee" or LIKE "bee" in different ways?

BTW, nice Help Link "HASH", I should read it before asking questions ;-))
Title: Re: XML Parser
Post by: ScriptBasic on July 04, 2017, 05:28:01 PM
Something LIKE this?

Code: [Select]
dict{"A"} = 1
dict{"B"} = "bee"
dict{"C"} = "sea"
dict{"D"} = "peebee"

FOR x = 0 TO UBOUND(dict) STEP 2
  IF dict[x+1] = "bee" OR dict[x+1] LIKE "*bee" THEN PRINT "Found: Key = ",dict[x],"  ",dict[x+1],"\n"
NEXT


jrs@jrs-laptop:~/sb/examples/test$ time scriba findval.sb
Found: Key = B  bee
Found: Key = D  peebee

real   0m0.005s
user   0m0.004s
sys   0m0.000s
jrs@jrs-laptop:~/sb/examples/test$


I had added Array Sort (http://www.scriptbasic.org/forum/index.php/topic,330.msg1471.html#msg1471) to the T(ools) extension module but it can only sort one index at a time and removes duplicate values.



Title: Re: XML Parser
Post by: ScriptBasic on July 08, 2017, 06:01:58 AM
I had an idea of using an associative array as a keyed file system index with Script BASIC's record based file I/O. The only thing I'm missing is a way to traverse the keys in sorted order forward and back. Direct key access is already covered. Any ideas?
Title: Re: XML Parser
Post by: B+ on July 08, 2017, 02:43:17 PM
I had an idea of using an associative array as a keyed file system index with Script BASIC's record based file I/O. The only thing I'm missing is a way to traverse the keys in sorted order forward and back. Direct key access is already covered. Any ideas?

Hi John,

If your inquiry is addressed to me, I have to say sorry there are too many technical phrases in the above quote that are not part of my experience to give ideas.

I have heard of this term: "associative array", interestingly enough, from someone at FB forum who was commenting on something they found at ScriptBasic forum.

"a keyed file system index"
"Script BASIC's record based file I/O"
"traverse the keys in sorted order forward and back"

Jargon talk Yikes! but I will take a stab at last item with a question.

Isn't the point of a key / value structure (like a hash table?) to get away from having to sort things out?

 
APPEND: Dang! I missed your July 4 EDIT, the above reply was made not having read the new link in that edit.
Title: Re: XML Parser
Post by: ScriptBasic on July 08, 2017, 05:36:50 PM
I think I can use my array sort function to create indexes for the records in the file. I'll post something soon to show what I'm talking about.

FYI - Hash tables / Associative arrays are in the order they were entered. I'll have a primary sorted index which is based on the associative element. (key) Other indexes could be built from fields in the data file record.
Title: Re: XML Parser
Post by: ScriptBasic on July 09, 2017, 05:51:38 PM
On second thought trying to recreate an ISAM keyed file system is like trying to keep Z80 programming alive. SQLite is all you need for a local DB solution,

Associative arrays or a combination of both can create some powerful matrices to work with non-normalized data sets.
Title: Re: XML Parser
Post by: ZXDunny on July 09, 2017, 07:53:17 PM
On second thought trying to recreate an ISAM keyed file system is like trying to keep Z80 programming alive.

I don't think there's any effort in that at all - z80 programming is very much alive and well on various platforms - both retro and embedded. z80 CPUs are still being manufactured.
Title: Re: XML Parser
Post by: ScriptBasic on July 09, 2017, 09:45:20 PM
Maybe not a good analogy but I don't think anyone would be interested in a Btrieve like file system these days.
Title: Re: XML Parser
Post by: ScriptBasic on July 10, 2017, 04:15:21 PM
To get back on topic, I feel the Script BASIC SPLITA and LIKE combo makes for a great lightweight XML parsing solution. My next goal is to get this to work with JSON.
Title: Re: XML Parser
Post by: ScriptBasic on August 13, 2017, 08:33:01 AM
Here is an example of transforming the QuickBooks Online Apps Directory (https://appcenter.intuit.com/allapps) to a simple table list (http://scriptbasic.org/home/qbo_apps) using the Script BASIC application proxy server.

Code: [Select]
' QBO Apps List

IMPORT cgi.bas
IMPORT curl.bas

ch = curl::init()
curl::option(ch,"URL","https://appcenter.intuit.com/allapps")
apps_html = curl::perform(ch)
curl::finish(ch)

cgi::Header 200,"text/html"
cgi::FinishHeader

PRINT """
<html>
<body>
<table>
<center><h1>QBO Apps Listing</h1></center>
<table style="width:100%">
  <tr>
    <th>App Name</th>
    <th>Vendor</th>
    <th>Description</th>
  </tr>
"""

SPLITA apps_html BY "<!-- this is for SEO crawlers -->" TO apps_array

FOR x = 0 TO UBOUND(apps_array)
  IF apps_array[x] LIKE """*<div class="bigappcard-display-name">*</div>*<div class="bigappcard-vendor-name">*</div>*<div class="bigappcard-tagline">*</div>*""" THEN
    PRINT "  <tr>\n"
    PRINT "    <td>",TRIM(joker(2)),"</td>\n"
    PRINT "    <td>",MID(TRIM(joker(4)),4),"</td>\n"
    PRINT "    <td>",TRIM(joker(6)),"</td>\n"
    PRINT "  </tr>\n"
  END IF
NEXT
PRINT """
</table>
</body>
</html>
"""


Title: Re: XML Parser
Post by: ScriptBasic on August 18, 2017, 06:02:20 AM
I'm curious what web development interest is here on the forum? It seems Windows desktop is where BASIC calls home.

Title: Re: XML Parser
Post by: ZXDunny on August 18, 2017, 10:33:19 AM
I'm on a mac...