Android Question Android 9 Error while Parsing .XML

Microinvest

Member
Licensed User
Hello there,

recently we tested existing and working code in Android 9.0 Environment. However the code is returning this error:

B4X:
main_parsefileforgroup (java line: 428)
org.apache.harmony.xml.ExpatParser$ParseException: At line 6, column 14: not well-formed (invalid token)
   at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:519)
   at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:478)
   at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:316)
   at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
   at anywheresoftware.b4a.objects.SaxParser.parse(SaxParser.java:80)
   at anywheresoftware.b4a.objects.SaxParser.Parse(SaxParser.java:73)
   at b4a.example.main._parsefileforgroup(main.java:428)
   at b4a.example.main._creategroups(main.java:393)
   at b4a.example.main._initialize(main.java:415)
   at b4a.example.main._activity_create(main.java:349)
   at java.lang.reflect.Method.invoke(Native Method)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
   at b4a.example.main.afterFirstLayout(main.java:104)
   at b4a.example.main.access$000(main.java:17)
   at b4a.example.main$WaitForLayout.run(main.java:82)
   at android.os.Handler.handleCallback(Handler.java:891)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:207)
   at android.app.ActivityThread.main(ActivityThread.java:7539)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
And this is the code of the Project:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A test parsing Android9
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True   
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public GroupNames As Map
    Public ReportsData As Map
    
    Private XML As SaxParser
    Private XMLText As InputStream
    Private Content As String
    
    Private strFileName As String
    Private strReportTitle As String
    Private FileDirectory As String   
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")   
    Initialize
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Class_Globals
    
End Sub

Public Sub Initialize
    XML.Initialize   
    GroupNames.Initialize
    ReportsData.Initialize
    CreateGroups
End Sub

Public Sub CreateGroups
    Dim FilesList As List                       
    FilesList.Initialize
    FilesList.AddAll(File.ListFiles(File.DirAssets))   
    For Each s As String In FilesList
        If(s.EndsWith(".xml")) Then               
            Log("show " & s)           
            ParseFileForGroup(s)               
        End If
    Next
End Sub

Private Sub ParseFileForGroup(FileName As String)
    strFileName = FileName   
    FileDirectory = File.DirAssets   
    XMLText = File.OpenInput(FileDirectory, FileName)
    XML.parse(XMLText, "XML")                       
    XMLText.Close
    
End Sub


Private Sub xml_StartElement (Uri As String, Name As String, Attributes As Attributes)
End Sub

Private Sub xml_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If Name.EqualsIgnoreCase("Title") Then               
        strReportTitle =  Text.ToString       
        ReportsData.Put(strReportTitle,strFileName)       
    End If
    
    If Name.EqualsIgnoreCase("Group") Then               
        Content = Text.ToString
        Dim Parts() As String
        Dim GroupName As String
        
        If Content.Contains("\") Then
            Parts = Regex.Split("\\",Content)
            GroupName = Parts(0)
        Else
            GroupName = Content
        End If
        'Log(GroupName)
        If Not(GroupNames.ContainsKey(GroupName)) Then 
            Dim tmplist As List
            tmplist.Initialize
            tmplist.Add(strReportTitle)
            GroupNames.Put(GroupName,tmplist)
        Else                                               
            Dim existinglist As List = GroupNames.Get(GroupName)
            existinglist.Add(strReportTitle)
            GroupNames.Put(GroupName,existinglist)
        End If
    End If
End Sub
After testing with xml files I got to conclusion that no matter if file is correctly formatted (correct xml file) or incorrect, the error is same. Xml Files are queries to databases and the code work fine as well. With Android Emulator the code is working without errors.

We also noticed the new rules applied for Android 9 - https://developer.android.com/preview/privacy/scoped-storage - maybe it is related?

Thnaks in advance.
 

agraham

Expert
Licensed User
We also noticed the new rules applied for Android 9 - https://developer.android.com/preview/privacy/scoped-storage - maybe it is related?
If I read it correctly the change is not for Android 9 but for the next version. Your linked article contains the line
apps that target Android 9 (API level 28) or lower see no change, by default, to how storage works from previous Android versions
So no, it's not related - but there may be other changes in Android 9 over earlier versions that are causing this :(
 

Microinvest

Member
Licensed User
Hello there,

I'm attaching the example project - in the moment it just logs the xml files, but the principle is the same. On Android 9 it crashes with the error in my previous post, with previous Android versions - it works.

Cheers

P.S. In the example project I put xml file as well.
 

Attachments

Top