Android Question xml read

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
i searched a lot about method enable me to load XML file to sqlite
itried this code
B4X:
Dim xm As Xml2Map
    xm.Initialize
    Dim root As Map = xm.Parse(File.ReadString(File.DirRootExternal, "css2.xml"))
 
       
        Dim mp As Map= root.Get("NewDataSet")
        Dim entries As List = mp.Get("Table")'<----error line
    DBUtils.InsertMaps(SQL, "css2", entries)
and my xml code is
<?xml version="1.0" encoding="UTF-8"?>
<NewDataSet>
<Table>
<sq>10002134</sq>
<nm>eeeeeee</nm>
<ar>56</ar>
<crd>02110050704</crd>
<crdtyp>1</crdtyp>
<ph>0</ph>
<dlv>0</dlv>
<gr>2</gr>
<mr>0</mr>
<dc>0</dc>
<nw>0</nw>
<prntd>6</prntd>
</Table>
</NewDataSet>
and the error was
Caused by: java.lang.RuntimeException: java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map$MyMap cannot be cast to java.util.List
i tried to modify the XML file but no benefit
i hope find solution
thank in advance
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
B4X:
 Dim entries As Map = mp.Get("Table")' It returns a MAP, not a LIST!
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
@DonManfred
how to get the list from in side the map???
i tries the
B4X:
Dim entries As Map = mp.values
but it was the same error
i copied that code example works successfully and the xml file was the same
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Dim entries As List = mp.Get("Table")'<----error line
raises an error.
Caused by: java.lang.RuntimeException: java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map$MyMap cannot be cast to java.util.List

i told you the solution.

you are now using another code. Another issue.

You are not giving enough informations. :-(
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but it was the same error
I strongly suggest to watch the collections tutorial video to understand the difference between lists and maps and their keys and/or values.
B4X:
Dim mp As Map= root.Get("NewDataSet")
for each k as string in mp.Keys
  log(k&"="&mp.get(k))
next

What is the output?
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
I strongly suggest to watch the collections tutorial video to understand the difference between lists and maps and their keys and/or values.
B4X:
Dim mp As Map= root.Get("NewDataSet")
for each k as string in mp.Keys
  log(k&"="&mp.get(k))
next

What is the output?

the out is :
Table={sq=10002134, nm=محمد حسين علي الشيباني, ar=56, crd=02110050704, crdtyp=1, ph=0, dlv=0, gr=2, mr=0, dc=0, nw=0, prntd=6}
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
there is no LIST inside btw.

<?xml version="1.0" encoding="UTF-8"?>
<NewDataSet>
<Table>
<sq>10002134</sq>
<nm>eeeeeee</nm>
<ar>56</ar>
<crd>02110050704</crd>
<crdtyp>1</crdtyp>
<ph>0</ph>
<dlv>0</dlv>
<gr>2</gr>
<mr>0</mr>
<dc>0</dc>
<nw>0</nw>
<prntd>6</prntd>
</Table>
</NewDataSet>
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
the out is :
Table={sq=10002134, nm=محمد حسين علي الشيباني, ar=56, crd=02110050704, crdtyp=1, ph=0, dlv=0, gr=2, mr=0, dc=0, nw=0, prntd=6}
so mp does contain another map in the key "Table".

B4X:
Dim mp As Map= root.Get("NewDataSet")
Dim table As Map= mp.Get("Table")
for each k as string in table.Keys
  log(k&"="&table.get(k))
next

 
Last edited:
Upvote 0
Top