PDA

View Full Version : The power ob GRAB$ function



ErosOlmi
14-10-2017, 12:55
A script example showing the power of GRAB$ (http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?grab$.htm) function.
A simple function that can be used in many situations where you have data structured with delimiters like ... XML nodes.

Parsing and XML can be a complex task depending on what library you use.
Parsing simple XML structure can be easily achieved using GRAB$ (http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?grab$.htm)

In this script an XML buffer is downloaded from the web and parsed using GRAB$ (http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?grab$.htm)

Have fun.
Eros



#MinVersion 1.10.4

uses "Console"


PrintL "-----------------------------------------------------------------" in %CCOLOR_FYELLOW
PrintL "---Load XML from https://www.w3schools.com/xml/cd_catalog.xml ---" in %CCOLOR_FYELLOW
PrintL "---Using winhttp.winhttprequest.5.1 COM Object ---" in %CCOLOR_FYELLOW
PrintL "---And parse string buffer (XML) using GRAB$ ---" in %CCOLOR_FYELLOW
PrintL "-----------------------------------------------------------------" in %CCOLOR_FYELLOW
PrintL "---WinHttpRequest object reference: ---" in %CCOLOR_FYELLOW
PrintL "---https://msdn.microsoft.com/en-us/library/windows/desktop/aa384106(v=vs.85).aspx" in %CCOLOR_FYELLOW
PrintL "-----------------------------------------------------------------" in %CCOLOR_FYELLOW


iDispatch oHTTP
String sXML
string sCD
string sTitle
long lCDIndex


printl "Creating winhttp.winhttprequest.5.1 object ..."
ohttp = NewCom("winhttp.winhttprequest.5.1")


if IsComObject(oHttp) then


printl "Object creation was ok"
printl "Now Getting XML ..." in %CCOLOR_FYELLOW


'---Open connection, send request, get response
oHTTP.Open("GET", "https://www.w3schools.com/xml/cd_catalog.xml")
oHTTP.Send
sXML = oHTTP.Responsetext


PrintL "All done." in %CCOLOR_FYELLOW
PrintL "---Press a key to print XMl content---" in %CCOLOR_FYELLOW
WaitKey


'---Print XML content
printl sXML

printl "---Now Parsing XML using GRAB$..." in %CCOLOR_FYELLOW
PrintL "---Press a key to start---" in %CCOLOR_FYELLOW
WaitKey

'---Loop till we have a <CD> node
lCDIndex = 1
Do
'---Grab all <CD></CD> nodes
sCD = grab$(sXML, "<CD>", "</CD>", lCDIndex)

if len(sCD) Then
'---From <CD> node get <TITLE> node
sTitle = grab$(sCD, "<TITLE>", "</TITLE>")


printl lCDIndex, "CD Title:", sTitle in %CCOLOR_FCYAN
printl $tab, "<CD> node content" in %CCOLOR_FLIGHTMAGENTA
printl sCD


incr lCDIndex
end If

loop while len(sCD)

'---Release object
oHttp = nothing

Else
PrintL "Error creating winhttprequest.5.1 COM Object" in %CCOLOR_FLIGHTRED
end if


PrintL "---All done, press a key to end---" in %CCOLOR_FYELLOW


WaitKey

primo
15-10-2017, 09:29
can't get it to work using online grabbing, it is possible a windows xp issue, will try tomorrow in windows 7
but i have downloaded the cd_catalog.xml file and loaded it directly with File_Load and after Parsing XML using GRAB$ it displayed the attached result in txt, the parsing results begins from line 37:
so it grabs first what is between CD and /CD and if it is true it grabs what is between Title and /Title specially and print it, then it display what is inside CD and /CD
i think it is a great tool, and it is the huge number of string functions which makes me use thinbasic. other compilers albeit good in making games but thinbasic is useful to Do the research and the scientific things

ErosOlmi
15-10-2017, 14:48
Ciao primo,

I think it is an HTTPS issue under Windows XP.
Try use this url (that uses HTTP instead of HTTPS) to download the XML buffer: http://www.xmlfiles.com/examples/cd_catalog.xml

Eros

primo
15-10-2017, 15:05
Thank you Eros
yes now it works with http://www.xmlfiles.com....