Results 1 to 4 of 4

Thread: The power ob GRAB$ function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    The power ob GRAB$ function

    A script example showing the power of GRAB$ 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$

    In this script an XML buffer is downloaded from the web and parsed using GRAB$

    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
    
    Last edited by ErosOlmi; 14-10-2017 at 12:57.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Similar Threads

  1. The Power of Nightmares
    By Charles Pegge in forum Shout Box Area
    Replies: 5
    Last Post: 23-05-2012, 07:45
  2. The power of ...
    By ErosOlmi in forum Shout Box Area
    Replies: 3
    Last Post: 05-01-2012, 08:36
  3. power sum evaluations
    By danbaron in forum Math: all about
    Replies: 7
    Last Post: 01-09-2011, 07:09
  4. power-tower function
    By danbaron in forum Science
    Replies: 0
    Last Post: 29-05-2011, 05:32
  5. Power supply calculator
    By Michael Clease in forum General
    Replies: 8
    Last Post: 12-04-2008, 08:36

Members who have read this thread: 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •