[Gambas-user] How to Disassemble XML/HTML

John Rose john.aaron.rose at mailbox.org
Sat Aug 22 20:02:36 CEST 2020


On 22/08/2020 11:12, user-request at lists.gambas-basic.org wrote:
> The existence of a key in a collection can be tested using the 
> Collection.Exist method. For example:
> If cEpisode.Exist("partOfSeries") Then
>   aSeriesName.Add(cEpisode["partOfSeries"]["name"])
>   aSeriesDescription.Add(cEpisode["partOfSeries"]["description"])
> End If
>
> You don't necessarily *need* to understand the JSON data format, but 
> it might help you determine which Gambas datatype is holding the 
> particular info you want after the conversion by JSON.Decode. 
> http://gambaswiki.org/JSON JSON elements & Gambas 
> datatypeswiki/comp/gb.util.web/json might help with that determination.
>
> While taking a look at the data, I see that some elements can contain 
> null values. For that reason, you may wish to use JSONCollection since 
> it allows for that; while Collection does not. For example: Dim 
> cEpisode As JSONCollection
> JSONCollection has the same properties and methods as Collection, so 
> it is virtually a 'drop-in' replacement.
>
> And just FYI, a scalar variable can be defined in the same line as 
> which it is declared:
> Dim sTextContent As String = "" 

Lee,

Thanks for the check on Exist of a key in a collection. That worked OK.

I've looked at the JSON web pages you mentioned. I remember the method 
of specification from using Pascal over 30 years ago. The correspondence 
between JSON elements & Gambas data types (in 
http://gambaswiki.org/wiki/comp/gb.util.web/json) is useful to know. The 
big problem I find with using Gambas for the HTML/XML/JSON stuff is not 
finding any examples and/or a book/tutorial on the usage of the various 
methods etc.

You say that I may wish to use JSONCollection (as opposed to Collection) 
as it allows for null values in some elements. I presume that that can 
be tested for using Gambas IsNull 'command'. JSON Collection is not 
quite a drop in replacement for Collection. When I did that (in 
ExtractEpisodes partly shown below) it gave a runtime error: wanted 
JSONCollection, got Collection. So I changed Collection to 
JSONCollection for the Dim cAEpisodes line and got the same. Do I have 
to do something else for the Extract procedure partly shown below:

Private Procedure ExtractEpisodes()
   Dim caEpisodes As Collection[]
   Dim cEpisode As JSONCollection
   Dim sTextContent As String
   sTextContent = ""
   If hData.Count = 0 Then
     QuitAfterError("No Episodes in Week " & sWeekNumber, "for " & 
sConnectMedium & " " & sConnectChannel)
   End If
   caEpisodes = hData[1]["@graph"]
   For Each cEpisode In caEpisodes

Public Sub Extract()
   Dim hHtmlDocument As New HtmlDocument(sDirectory & "/HTMLandXML.txt")
   Dim hXmlElement As XmlElement
   For Each hXmlElement In hHtmlDocument.GetElementsByTagName("script")
     If hXmlElement.Attributes["type"] <> "application/ld+json" Then 
Continue
     hData.Add(JSON.Decode(hXmlElement.TextContent))
   Next

I know about giving variables initial values in Dim & Private 
statements. I tend not to do so as I normally have a routine called from 
Form_Open & elsewhere to do that when an app effectively starts from the 
beginning again. This often makes testing easier for me. Also, it's a 
'hangover' from writing in languages like Cobol & Fortran.




More information about the User mailing list