Android Question Xml2Map - Compilation error

Indy

Active Member
Licensed User
Longtime User
Hi,

I'm trying to use Xml2Map class to get values from XML that is passed back from a WCF. I followed the example in the first post to loop through the data from the XML string but, changed the XML references as per my output, however I get a compilation error as follows;

B4X:
Compiling generated Java code.    Error
B4A line: 105
For Each res As Map In root.Get(\
javac 1.8.0_131
src\b4a\example\main.java:485: error: incompatible types: Object cannot be converted to IterableList
group26 = _root.Get((Object)("GetDataResponse"));

Here's the XML response from the WCF;

B4X:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <ActivityId CorrelationId="8709d988-7118-46e3-a65f-4d81be46993e" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">a98d95ae-573d-4636-aadc-738ee383269e</ActivityId>
  </s:Header>
  <s:Body>
    <GetDataResponse xmlns="http://tempuri.org/">
      <GetDataResult>You entered: 100</GetDataResult>
    </GetDataResponse>
  </s:Body>
</s:Envelope>

The B4X code I'm using is as follows;

B4X:
Dim xm As Xml2Map
xm.Initialize
       
ParsedData = xm.Parse(strXMLResponse)
       
Dim root As Map = ParsedData.Get("Body")     
           
For Each res As Map In root.Get("GetDataResponse")
         
Next

Have I missed something in my code?

Thanks.
 

Indy

Active Member
Licensed User
Longtime User
Hi Erel,

ParseData contains

B4X:
(MyMap) {Envelope={Header={ActivityId={Attributes={CorrelationId=8e40d0fb-0e7b-4d33-92c9-795877a52929}, Text=ed778225-a20f-4c6f-8410-acef76d1217d}}, Body={GetDataResponse={GetDataResult=You entered: 11}}}}

I'll be honest with you, what puzzles me is why the error during compilation? Surely it can't know what XML tags to expect until the app actually runs. The only thing I can think of is, all the examples for this load an XML file whereas I want to pass in XML. Do you think the complier loads in the file before and then validates the tags at compilation time?

Thanks.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
'll be honest with you, what puzzles me is why the error during compilation?
This line cannot be compiled as the compiler cannot use an Object in a For Each loop.
B4X:
For Each res As Map In root.Get("GetDataResponse")

The first step is to format the maps data:
SS-2018-03-05_12.35.35.png


To get the "GetDataResult" element:
B4X:
Dim Envelope As Map = ParsedData.Get("Envelope")
Dim Body As Map = Envelope.Get("Body")
Dim GetDataResponse As Map = Body.Get("GetDataResponse")
Dim result As String = GetDataResponse.Get("GetDataResult")
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
Hi Erel,

I see where I was going wrong. I have used your code and it works. However, what I've now done is to try and extend that to loop through a collection of data. So, for example I now have the following data (from ParseData)

B4X:
(MyMap) {Envelope={Header={ActivityId={Attributes={CorrelationId=06889bd3-b674-4db9-aa51-dcb216e3a85e}, Text=10c59f8d-a4c8-4e0e-afa2-369aa57722e7}}, Body={GetDataListResponse={GetDataListResult={string=[A, B, C]}}}}}

I tried the following code but got an error.

B4X:
Dim Envelope As Map = ParsedData.Get("Envelope")
Dim Body As Map = Envelope.Get("Body")
Dim GetDataListResponse As Map = Body.Get("GetDataListResponse")
Dim GetDataListResult As Map = GetDataListResponse.Get("GetDataListResult")
Dim GetDataListValues As Map = GetDataListResult.Get("string")
           
For Each res As String In GetDataListValues.Values
    Msgbox(res,"")           
Next

B4X:
main$ResumableSub_Button1_Clickresume (java line: 525)
java.lang.ClassCastException: java.util.ArrayList cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap
at b4a.example.main$ResumableSub_Button1_Click.resume(main.java:525)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1135)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)

Can you tell where I've gone wrong?

Thanks.
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
Hi DonManfred

Worked perfectly! Now I'm able to retreive single and multiple values from the WCF service. :)

Thank you both Erel and DonManfred for your help.

Thanks
 
Upvote 0
Top