Android Question XML from DataSet .net to MAP (SOLVED )

netsistemas

Active Member
Licensed User
i have a webservice in .net than return a xml simil to this.

my final object is replace a proyect, now with jdbresulset sql server (Direct connect), replece using webservice.

my idea is use MAP (or may may be create a jdbcresult or resultset from xml)
the first step, i think is get a correct map.

(i am using this guide: https://www.b4x.com/android/forum/threads/b4x-xml2map-simple-way-to-parse-xml-documents.74848/)


B4X:
<DataTable xmlns="http://tempuri.org/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
...
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<DocumentElement xmlns="">
<Proveedor diffgr:id="Proveedor1" msdata:rowOrder="0">
...
</Proveedor>
<Proveedor diffgr:id="Proveedor2" msdata:rowOrder="1">
...
</Proveedor>
<Proveedor diffgr:id="Proveedor3" msdata:rowOrder="2">
...
</Proveedor>
<Proveedor diffgr:id="Proveedor4" msdata:rowOrder="3">
...
</Proveedor>
<Proveedor diffgr:id="Proveedor5" msdata:rowOrder="4">
<Proveedor>4472</Proveedor>
<NombreProveedor>FRIC</NombreProveedor>
</Proveedor>
<Proveedor diffgr:id="Proveedor6" msdata:rowOrder="5">
<Proveedor>3072</Proveedor>
<NombreProveedor>Ga.</NombreProveedor>
</Proveedor>
<Proveedor diffgr:id="Proveedor7" msdata:rowOrder="6">
<Proveedor>3473</Proveedor>
<NombreProveedor>KILU</NombreProveedor>
</Proveedor>
<Proveedor diffgr:id="Proveedor8" msdata:rowOrder="7">
<Proveedor>3052</Proveedor>
<NombreProveedor>Peco.</NombreProveedor>
</Proveedor>
<Proveedor diffgr:id="Proveedor9" msdata:rowOrder="8">
<Proveedor>3977</Proveedor>
<NombreProveedor>SaltS.A.</NombreProveedor>
</Proveedor>
<Proveedor diffgr:id="Proveedor10" msdata:rowOrder="9">
<Proveedor>3017</Proveedor>
<NombreProveedor>Salvoda S.A.</NombreProveedor>
</Proveedor>
<Proveedor diffgr:id="Proveedor11" msdata:rowOrder="10">
<Proveedor>4003</Proveedor>
<NombreProveedor>SUMINISTROS RICIDAD</NombreProveedor>
</Proveedor>
<Proveedor diffgr:id="Proveedor12" msdata:rowOrder="11">
<Proveedor>3157</Proveedor>
<NombreProveedor>Suministros </NombreProveedor>
</Proveedor>
</DocumentElement>
</diffgr:diffgram>
</DataTable>


How parse this code to resulset or map (this is my initial code) but get datatable no return data:


B4X:
    If J.Success Then
 
            Dim ParsedData As Map
            Dim X2M As Xml2Map
            X2M.Initialize
            ParsedData = X2M.Parse(J.GetString)
            Dim Mapa2 As Map
            Mapa2 = ParsedData.Get("DataTable")
            Dim Mapa3 As Map
            Mapa3 = Mapa2.Get("diffgr:diffgram")

        Dim items As List
        items = Mapa3.Get("DocumentElement")
        For Each item As Map In items
            Dim P As String = item.Get("Proveedor")
            Dim N As String = item.Get("NombreProveedor")
            Log(N)
        Next
 
Last edited:

netsistemas

Active Member
Licensed User
Finally I have achieved it, but the level of depth within the XML is very great. To understand it, it is very important to use JSONGENERATOR and see the tree it mounts. Then you must analyze the branches of the XML and know which one you want to enter and their names. In the code that I attach, it should be noted that when I get to branch number 5 or better said, I extract the corresponding MAP, I extract number 2, since 1 has the schema. Then, when taking my SUPPLIER branch, effectively in The JSON can be seen as an array returns (see the bracket: [) and within each element of the array, we effectively have a new map. Each XML is logically different, but the one that I have analyzed is a DATASET that the WEB Service returns.

B4X:
public Sub GetData() As ResumableSub
    Dim J As HttpJob
    Dim RequestSoapXML As String
    J.Initialize("",Me)
    
    
    RequestSoapXML =$"<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetProveedores xmlns="http://tempuri.org/" />
  </soap:Body>
</soap:Envelope>"$

Dim url As String

    
    url = "http://www.dominio.com/proveedor.asmx"
    j.PostString(url,RequestSoapXML)
    j.GetRequest.SetContentType ("text/xml; charset=utf-8")
    j.GetRequest.SetHeader("Content-Type", "text/xml; charset=utf-8")
    

    
        wait for (J) JobDone(j As HttpJob)
    
    Try

    
    If J.Success Then
    
            Dim ParsedData As Map
            Dim X2M As Xml2Map
            X2M.Initialize
            ParsedData = X2M.Parse(J.GetString)
            Log (ParsedData.GetKeyAt(0))
            Dim map2 As Map
            map2 = ParsedData.Get("Envelope")
            Log(map2.GetKeyAt(0))
            
    
            Dim map3 As Map
            map3 = map2.Get("Body")
            Log(map3.GetKeyAt(0))
            Dim Map4 As Map
            Map4 = map3.get("GetProveedoresResponse")
            Dim map5 As Map
            Log(Map4.GetKeyAt(0))
            map5 = Map4.Get("GetProveedoresResult")
            Log(map5.GetKeyAt(1)) '0:schema 1:diffgram
            Dim map6 As Map
            map6 = map5.Get("diffgram")
            Log(map6.GetKeyAt(0))
            Dim map7 As Map
            map7 = map6.Get("DocumentElement")
            Log(map7.GetKeyAt(0))
    
            
            Dim F As Int
            Dim items As List
            items = map7.Get("Proveedor")
For Each item As Map In items
    Log(item.Get("NombreProveedor"))
Next
            
            
            Dim Jg As JSONGenerator
            Jg.Initialize(ParsedData)
            Log(Jg.ToPrettyString(3))   
        
'            Log(J.GetString)
        Else
            Log("Error:" & J.ErrorMessage)
            
        End If
    
            
    Catch
        Log(LastException)
    End Try
    
    
    
    J.Release
    
    Return True
    
End Sub

Next step -> map to jdbcresult
 
Upvote 0
Top