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
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