Android Question Read XML array output with key/value

phukol

Active Member
Licensed User
Longtime User
Hi guys i just want to ask how can i read an XML file having key/value data using Parser? my XML has this pattern :
B4X:
<ns1:getuserinfoResponse>
<return SOAP-ENC:arrayType="ns2:Map[1]" xsi:type="SOAP-ENC:Array">
 <item xsi:type="ns2:Map">
    <item>
         <key xsi:type="xsd:string">id</key>
         <value xsi:type="xsd:string">26</value>
   </item>
   <item>
             <key xsi:type="xsd:string">user_id</key>
            <value xsi:type="xsd:string"></value>
     </item>
 

walterf25

Expert
Licensed User
Longtime User
Hi guys i just want to ask how can i read an XML file having key/value data using Parser? my XML has this pattern :
B4X:
<ns1:getuserinfoResponse>
<return SOAP-ENC:arrayType="ns2:Map[1]" xsi:type="SOAP-ENC:Array">
<item xsi:type="ns2:Map">
    <item>
         <key xsi:type="xsd:string">id</key>
         <value xsi:type="xsd:string">26</value>
   </item>
   <item>
             <key xsi:type="xsd:string">user_id</key>
            <value xsi:type="xsd:string"></value>
     </item>
What have you tried so far?

there are plenty of examples if you do a search for XML parser.

Cheers,
Walter
 
Upvote 0

phukol

Active Member
Licensed User
Longtime User
Thanks but of course i already searched first before asking anything. I have tried this method :
B4X:
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    
    If parser.Parents.IndexOf("getuserinfoResponse") > -1 Then 'get user info
        If Name = "HOUSE_NUM" Then
            MainMenu.strAddress = Text.ToString + MainMenu.strAddress
        Else If Name = "BRGY_" Then
            MainMenu.strAddress = Text.ToString + MainMenu.strAddress
        Else If Name = "CITY_" Then
            MainMenu.strAddress = Text.ToString + MainMenu.strAddress
        Else If Name = "LNAME_" Then
            MainMenu.strCardName = Text.ToString + MainMenu.strCardName
            MainMenu.strLName = Text.ToString
        Else If Name = "FNAME_" Then
            MainMenu.strCardName = Text.ToString + MainMenu.strCardName
            MainMenu.strFName = Text.ToString
        Else If Name = "MNAME_" Then
            MainMenu.strCardName = Text.ToString + MainMenu.strCardName
            MainMenu.strMName = Text.ToString
        Else If Name = "CARD_NUM" Then
            MainMenu.strCardNum = Text.ToString
        Else If Name = "EMAIL_" Then
            MainMenu.strEmail = Text.ToString 
        Else If Name = "TEL_NUM" Then
            MainMenu.strTel = Text.ToString 
        Else If Name = "maiden_name" Then
            MainMenu.strMaidenName = Text.ToString   
        End If
    End If
End Sub

but its not able to access the "Name" attribute. i tried comparing my logs because i was already successful in reading XML files with this format :

B4X:
<ns1:loginResponse>
<return xsi:type="SOAP-ENC:Struct">
<result xsi:type="xsd:string">4</result>
<transid xsi:type="xsd:string">1001312073</transid>
<msg xsi:type="xsd:string">Session Created</msg>
<session xsi:type="xsd:string">JDO6L913AQ779MH1CATS</session>
</return></ns1:loginResponse>

using the pattern used above
 
Upvote 0

phukol

Active Member
Licensed User
Longtime User
For anyone having issues with the xml pattern
B4X:
  <item>
         <key xsi:type="xsd:string">id</key>
         <value xsi:type="xsd:string">26</value>
   </item>
   <item>
             <key xsi:type="xsd:string">user_id</key>
            <value xsi:type="xsd:string"></value>
     </item>

what i did was modify the parser sub to this :

B4X:
If parser.Parents.IndexOf("getuserinfoResponse") > -1 Then 'get user info
  
      
        If parser.Parents.IndexOf("item") > -1 Then
            If Name = "key" Then
                strKey = Text.ToString
            Else If Name = "value" Then
                If strKey = "id" Then
                    MainMenu.strID = Text.ToString
                else If strKey = "HOUSE_NUM" Then
                    MainMenu.strAddress = Text.ToString & MainMenu.strAddress
                Else If strKey = "BRGY_" Then
                    MainMenu.strAddress = Text.ToString & MainMenu.strAddress
                Else If strKey = "CITY_" Then
                    MainMenu.strAddress = Text.ToString & MainMenu.strAddress
                Else If strKey = "LNAME_" Then
                    MainMenu.strCardName = Text.ToString & MainMenu.strCardName
                    MainMenu.strLName = Text.ToString
                Else If strKey = "FNAME_" Then
                    MainMenu.strCardName = Text.ToString & MainMenu.strCardName
                    MainMenu.strFName = Text.ToString
                Else If strKey = "MNAME_" Then
                    MainMenu.strCardName = Text.ToString & MainMenu.strCardName
                    MainMenu.strMName = Text.ToString
                Else If strKey = "CARD_NUM" Then
                    MainMenu.strCardNum = Text.ToString
                Else If strKey = "EMAIL_" Then
                    MainMenu.strEmail = Text.ToString
                Else If strKey = "TEL_NUM" Then
                    MainMenu.strTel = Text.ToString
                Else If strKey = "maiden_name" Then
                    MainMenu.strMaidenName = Text.ToString  
                End If
            End If
        End If

i declared strKey in Sub Globals as string. Basically i check all the possible Name values and store them in strKEy, then i check for the possible values using the "next" Name value
 
Upvote 0
Top