iOS Question Parsing XML and adding items to List

Moosi

Member
Licensed User
Longtime User
Hi,

I have a problem while parsing an XML.
This is what I am doing while parsing:

B4X:
Private Sub Parser_StartElement (uri As String, Name As String, Attributes As Map)
    If Name = "id" Then
        Dim Doc As MyDoc
        Doc.Initialize
        DocList.Add(Name)
    End If
End Sub

Private Sub Parser_EndElement (uri As String, Name As String, Text As StringBuilder)
    If Parser.Parents.IndexOf("Download") > -1 Then
        Dim Doc As MyDoc
        Doc = DocList.Get(DocList.Size - 1) 'ref to the last Doc
        Log(Name)
        Select Name
            Case "id"
                Doc.ID = Text.ToString
        End Select
    End If
End Sub

As you can see, I want to add the content of the XML to a List.

I works pretty well and fast until the XML gets larger than approx. 1MB
At some point I am getting this Error in the Log and the App crashes.


This is the Error-Log:
B4X:
Application_Start
Application_Active
Application_Inactive
SignalHandler 11
Error occurred on line: 69 (Main)
Signal - 11
Stack Trace: (
    "0   B4i xmlTest          SignalHandler + 112",
    "1   libsystem_platform.dylib            0x1b110251 _sigtramp + 16",
    "2   ???                  0x0 + 0",
    "3   B4i xmlTest          -[B4IDebugDataOutputStream copyToBuffer:count:] + 86",
    "4   B4i xmlTest          -[B4IShellConnector writeObject:object:stringsCacheDisabled:] + 822",
    "5   B4i xmlTest          -[B4IShellConnector writeObject:object:] + 44",
    "6   B4i xmlTest          -[B4IShellConnector writeList:list:] + 248",
    "7   B4i xmlTest          -[B4IShell raiseEventImpl:method:args::] + 1306",
    "8   B4i xmlTest          -[B4IShellBI raiseEvent:event:params:] + 1578",
    "9   B4i xmlTest          __33-[B4I raiseUIEvent:event:params:]_block_invoke + 74"
)

Could this be a memory issue, because adding all the custom Types to a list is too much?
I added a testproject in case someone want to have a look.

Thanks
Moosi
 

Attachments

  • xml_test.zip
    1.5 KB · Views: 237

Moosi

Member
Licensed User
Longtime User
OK, obviously nobody has an idea, or my question lacks of information...

Is there a way to show the memory used on an iOS device?

thanks
Moosi
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Is there a way to show the memory used on an iOS device?
Yes, you can open the XCode project on a mac and use the memory monitor. However, I don't think it's a memory problem.

Please check your code again, I have added some comments:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
  
    Private Parser As SaxParser
    Private DocList As List
    Type MyDoc (ID As String, title As String ,Datei As String,Datum As String,Dokumententtyp As String,Sprache As String,Anwendungsbereich As String,Aktualisierungszeitpunkt As String,DtTop As String, Seitenzahl As String, keywords As String, Size As String)
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
    LadeXML 'Don't do such heavy process in the Application_Start sub
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
  
End Sub

Private Sub Application_Background
  
End Sub

Sub LadeXML()
    Dim j As HttpJob
    j.Initialize("XML", Me)
    j.Download("http://m-te.de/list_klein.xml")
    j.GetRequest.Timeout = 600000
End Sub

Private Sub JobDone (Job As HttpJob)
    If Job.Success = True Then
        CallSubDelayed2(Me,"ParseXML",Job.GetString) 'Why you are using CallSubDelayed here? --> This mistake is actually the reason for the error.
    End If
    Job.Release
End Sub

Private Sub ParseXML(Key As String)
    DocList.Initialize
    Dim In As InputStream 'You don't have to create a new InputStream, checkout the Parse2 Method of the parser
    Dim data() As Byte = Key.GetBytes("UTF8")
    In.InitializeFromBytesArray(data, 0, data.Length)
    Parser.Initialize
    Parser.Parse(In, "Parser")
    Log("End Parse")
End Sub

Private Sub Parser_StartElement (uri As String, Name As String, Attributes As Map)
    If Name = "id" Then
        Dim Doc As MyDoc
        Doc.Initialize
      
        DocList.Add(Name) 'Probably, here you wanted to add your Doc type, and you have forgotten Doc.ID = Name, right?
    End If
End Sub

Private Sub Parser_EndElement (uri As String, Name As String, Text As StringBuilder)
    If Parser.Parents.IndexOf("Download") > -1 Then
        Dim Doc As MyDoc
        Doc = DocList.Get(DocList.Size - 1) 'ref to the last Doc 'The last element in you List is a String type and not a Doc type
        Log(Name)
        Select Name
            Case "id"
                Doc.ID = Text.ToString
        End Select
    End If
End Sub



Jan
 
Upvote 0

Moosi

Member
Licensed User
Longtime User
Hey Jan,
many thanks for your help.
The project you see is an extract of an larger one. To keep it simple I put everything in the main module.
So you are right with all your comments, but they don´t apply to the real project except your point with the Parse2 method.
I will look into it, thanks for pointing this out.

Why I used CallSubDelayed2(Me,"ParseXML",Job.GetString) there? I don´t know, maybe a relikt.
It works as expected with just the call ParseXML(Job.GetString).

Do you have an idea why this is crashing? With a smaller XML it works.

Thanks again
moosi
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

Do you have an idea why this is crashing?
It's hard to say, as it works for me without an error (after improving the code). First of all, you should try the Xml2Map class Erel has posted. If this doesn't help, you can send me your project and I can have a look at it ...

Jan
 
Upvote 0

Moosi

Member
Licensed User
Longtime User
I will try the Xml2Map class.
Don´t worry, it works very well now, so there is no need to spend your time.
Anyway many thanks for your offer.

Moo
 
Upvote 0
Top