Android Question How right to parse a XML element

Valeriy Lakhtin

Member
Licensed User
Previously, I had a lot of problems with parsining of XML element with XOM library. The problem was that the XML Element should be written in a single line, as in example "first" group or 3 line as in "second" group.
B4X:
<?xml version="1.0" encoding="UTF-8"?>
<dataroot>
    <group g_name="first" option="Phone">
        <name>TAXI Rocet</name>
        <rating>55</rating>
    </group>
    <group g_name="second" option="Phone">
        <name>
            TAXI Speed
        </name>
        <rating>
            34
        </rating>
    </group>
</dataroot>
If the elements "name", "rating" is recorded in 3 lines we have are problems. But in both cases, the XML document is valid. Please give advice on how to better parse element which is written in 3 lines. How best to remove the LFCR, TAB, SPACE chars in string inside XML element.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Complete example:
B4X:
Sub Process_Globals
   Private sax As SaxParser
   Type Record (Name As String, Rating As Int)
   Private Records As List
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then   
     Records.Initialize
     sax.Initialize
     Dim in As InputStream = File.OpenInput(File.DirAssets, "1.xml")
     sax.Parse(in, "sax")
     in.Close
     Log(Records)
   End If
End Sub

Sub Sax_StartElement (Uri As String, Name As String, Attributes As Attributes)
   
End Sub

Sub Sax_EndElement (Uri As String, Name As String, Text As StringBuilder)
   If Name = "name" Then
     Dim r As Record
     r.Initialize
     r.Name = Text.ToString.Trim
     Records.Add(r)
   Else if Name = "rating" Then
     Dim r As Record = Records.Get(Records.Size - 1)
     r.Rating = Text.ToString.Trim
   End If
End Sub
 
Upvote 0
Top