[Gambas-user] How to Disassemble XML/HTML

T Lee Davidson t.lee.davidson at gmail.com
Sun Aug 23 05:57:08 CEST 2020


On 8/22/20 2:02 PM, John Rose wrote:
> 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:
> 
[snip]
> 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

Yes. You have to set the JSON.Decode UseNull flag so that it converts the JSON Object using the JSONCollection datatype instead 
of the standard Gambas Collection: hData.Add(JSON.Decode(hXmlElement.TextContent, True))

A Null value can be tested a number of ways: using IsNull(value), using an equality test (eg. If value = Null), or using 
negation (eg. If Not value).

Be aware that there are two ways you can handle the data containing null values.

1. Allow the data to be converted using the standard Gambas Collection and test for null values with the Collection.Exist 
method. (If a JSON Object element contains a null value, the element's key will not exist in the Collection as it will not get 
set by Gambas.)

2. Ensure the data is converted using JSONCollection datatype and test for null values with IsNull, =, or Not.


-- 
Lee


More information about the User mailing list