Parsing Apple property lists (commonly used by iOS apps)

99toLife

Member
Licensed User
Longtime User
Has anyone had any success parsing Apple property lists (commonly used by iOS apps)?

An example is below:

B4X:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
   <dict>
      <key>title</key>
      <string>The Title</string>
      <key>link</key>
      <string>http://link/</string>
   </dict>
   <dict>
      <key>title</key>
      <string>The Title2</string>
      <key>link</key>
      <string>http://link2/</string>
   </dict>
</array>
</plist>

I'm going crazy trying to display data from a plist into a list view. Any tips or suggestions are appreciated! :sign0163:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Process_Globals
   Dim parser As SaxParser
   Type Item (Title As String, Link As String)
   Dim Items As List
   Dim key As String
End Sub
Sub Globals

End Sub
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      parser.Initialize
      Items.Initialize
      Dim in As InputStream
      in = File.OpenInput(File.DirAssets, "list.xml")
      parser.Parse(in, "parser")
      in.Close
   End If
End Sub
Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
   If Name = "dict" Then
      Dim i As Item
      i.Initialize
      Items.Add(i)
   End If
End Sub
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
   If Name = "key" Then
      key = Text.ToString
   Else If Name = "string" Then
      Dim i As Item
      i = Items.Get(Items.Size - 1) 'get the last added item
      Select key
         Case "title"
            i.Title = Text.ToString
         Case "link"
            i.Link = Text.ToString
      End Select
   End If
End Sub
 
Upvote 0

sanjibnanda

Active Member
Licensed User
Longtime User
listview from plist

Here:
B4X:
Sub Process_Globals
   Dim parser As SaxParser
   Type Item (Title As String, Link As String)
   Dim Items As List
   Dim key As String
End Sub
Sub Globals

End Sub
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      parser.Initialize
      Items.Initialize
      Dim in As InputStream
      in = File.OpenInput(File.DirAssets, "list.xml")
      parser.Parse(in, "parser")
      in.Close
   End If
End Sub
Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
   If Name = "dict" Then
      Dim i As Item
      i.Initialize
      Items.Add(i)
   End If
End Sub
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
   If Name = "key" Then
      key = Text.ToString
   Else If Name = "string" Then
      Dim i As Item
      i = Items.Get(Items.Size - 1) 'get the last added item
      Select key
         Case "title"
            i.Title = Text.ToString
         Case "link"
            i.Link = Text.ToString
      End Select
   End If
End Sub

Hi Erel,
very nice to see that B4A can parser plist files too. I tried and it works, but how can i use above code to populate listview. Your help can definitely push me accelerated.:sign0085:

regards,
sanjib
 
Upvote 0

sanjibnanda

Active Member
Licensed User
Longtime User
listview with plist overlapping

After you parse the XML file you can go over the items in the list and add them to a list view.

thanks Erel for your help, i tried but the all list elements are overlapping.
Could you see where i am wrong in the attached zip file
B4X:
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
   ListView1.Initialize("ListView1")
   ListView1.SingleLineLayout.ItemHeight = 60dip
   Activity.AddView(ListView1, 0, 0, 100%x, 100%y)
    If Name = "key" Then
        key = Text.ToString
    Else If Name = "string" Then
        Dim i As Item
        i = Items.Get(Items.Size - 1) 'get the last added item
        Select key
            Case "title"
                i.Title = Text.ToString
            Title = i.Title
            
            Case "link"
                i.Link = Text.ToString
            Link = i.Link
        End Select
    End If
   
   ListView1.AddTwoLines(Title,Link) 'add the title as the text and the link as the value
End Sub


regards,
sanjib
 

Attachments

  • plist.zip
    309.4 KB · Views: 140
Upvote 0

sanjibnanda

Active Member
Licensed User
Longtime User
saxparser creates additional blank record with plist

Please use File - Export as zip when uploading projects. You should not initialize ListView1 for every item. You should only initialize it once and add it to the activity once. Your code creates a new ListView for every item.

Thanks Erel, I realized my mistake and now it loads, but the parser creates an additional null array at the begining and all records are loaded but one less.

Moreover, how could i load the bitmap with 'AddTwoLinesAndBitmap' from remote server?
regards,
sanjib
 

Attachments

  • plist.zip
    6.2 KB · Views: 143
Upvote 0
Top