When I try to parse attached text.xml (RSS) I'm getting error: java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map$MyMap cannot be cast to java.util.List
on line
B4X:
For Each item As Map In items
I have tested XML against various online validators everyone say it's a valid XML.
Anyone know what's wrong here?
I have found what is problem, If XML contain only one ITEM tag, then error is triggered. In this attached file there is two ITEM tags and parser work as expected.
Now the question is how to parse XML with only one ITEM tag inside.
If FirstTime Then
Dim xm As Xml2Map
xm.Initialize
ParsedData = xm.Parse(File.ReadString(File.DirAssets, "rss.xml"))
End If
Log(ParsedData)
For Each key As String In ParsedData.Keys
Log(ParsedData.Get(key))
Next
Ther content of the only key in the map is
{Attributes={version=2.0}, channel={title=1, link=2, description=3, language=4, pubDate=Mon, 16 Jan 2017 12:55:01 +0100, lastBuildDate=Mon, 16 Jan 2017 12:55:01 +0100, docs=http://www.example.com, webMaster=example@example.com, item={link=http://www.example.com, title=Test, description=test desc, pubDate=Mon, 16 Jan 2017 12:55:01 +0100}}}
Dim rss As Map = ParsedData.Get("rss")
Dim channel As Map = rss.Get("channel")
Dim items As List = channel.Get("item")
For Each item As Map In items '<--- error (java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map$MyMap cannot be cast to java.util.List)
Next
Xml2Map works properly here.
If there is a single element then it will return a Map. If there are multiple elements with the same name then it will return a List (the other option is to always return a List for all items).
In this case you need to add a check:
B4X:
if channel.Get("item") Is Map Then
'single item
Dim item As Map = channel.Get("item")
Else
Dim items As List = channel.Get("item")
For Each item As Map In items
...