Android Question XML -> MAP issue

yo3ggx

Active Member
Licensed User
Longtime User
This is just a simplified example to explain the issue I have.
I'm using XML2MAP to covert a simple xml file:

B4X:
<root>
    <config>
        <param1>val11</param1>
        <param2>val12</param2>
        <param3>val13</param3>
    </config>
    <config>
        <param1>val21</param1>
        <param2>val22</param2>
        <param3>val23</param3>
    </config>
</root>

And the following code:
B4X:
Dim Parsed As Map = xm.Parse(xmlfile)
Dim mp as Map = Parsed.Get("list")
Dim lst As List
lst = GetElements(mp,"config")
Log("lst size = " & lst.Size)
File.WriteMap(File.DirInternal,"example.map",mp)
mp = File.ReadMap(sLocalRepository,"example.map")
lst = GetElements(mp,"config")
Log("lst size = " & lst.Size)

And the routine GetElements
B4X:
Sub GetElements (m As Map, key As String) As List
    Dim res As List
    If m.ContainsKey(key) = False Then
        res.Initialize
        Return res
    Else
        Dim value As Object
         value = m.Get(key)
        Log(key & ": " & value)
        If value Is List Then Return value
        res.Initialize
        res.Add(value)
        Return res
    End If
End Sub

The problem is that if I save the map and theN I read it from the file, GetElements(mp,"config") does no more return a list, but a single element as a string.
Logging the two 'value' objects, looks absolutely the same.

I am doing something wrong?

Thank you
 
Last edited:

William Lancee

Well-Known Member
Licensed User
Longtime User
File.WriteMap converts all values to a string and saves one key: string-Value as a pair on each record/line in the file.
The List is converted to a string. When read it is still a string. When you log the list and string they look the same.

You need to use the B4XSerializator from the internal RandomAccessFile library.

B4X:
    Dim BS As B4XSerializator
    File.WriteBytes(File.DirApp,"example.txt", BS.ConvertObjectToBytes(mp1))
    Dim mp3 As Map = BS.ConvertBytesToObject(File.ReadBytes(File.DirApp,"example.txt"))
    Dim lst3 As List = GetElements(mp3,"config")
    Log(lst3.Size & TAB & lst3.Get(0))
 
Upvote 1

yo3ggx

Active Member
Licensed User
Longtime User
Is not duplicate. List in a map is working according to Erel solution, but if you save the map and load the map again from the file, the list of maps is lost. I will try William suggestion.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Is not duplicate. List in a map is working according to Erel solution, but if you save the map and load the map again from the file, the list of maps is lost. I will try William suggestion.
I see. Maybe I was confused with the title. It should have mentioned about saving and reading from file.
 
Upvote 0

yo3ggx

Active Member
Licensed User
Longtime User
This was the solution for my problem. Thank you.

I was not aware that WriteMap does not save the map in the right format. Then there is any difference between WriteMap and WriteString?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I prefer to work with JSON library. The format is human readable.

B4X:
Dim xmlfile As String = File.ReadString(File.DirApp, "example.xml")

Dim xm As Xml2Map
xm.Initialize
Dim Parsed As Map = xm.Parse(xmlfile)
'Log(Parsed.As(JSON).ToString)

Dim root As Map = Parsed.Get("root")
Dim config As List
config = GetElements(root, "config")
Log("lst size = " & config.Size)

File.WriteString(File.DirApp, "example.map", root.As(JSON).ToString)
Dim mapfile As String = File.ReadString(File.DirApp, "example.map")
'Log(mapfile)
config = GetElements(mapfile.As(JSON).ToMap, "config")
Log("lst size = " & config.Size)

Code only tested in B4J. You may change File.DirApp to File.Internal in B4A.
 
Upvote 0

yo3ggx

Active Member
Licensed User
Longtime User
To find out quickly what a method does, simply hover over its name and read the Intellisense.
I was aware that WriteMap create a text file, with list represented using '{}', but I expected that ReadMap do the reverse and reconstruct the list from the string.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…