B4J Question Reading value from XML

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I have having a small issue trying to read a XML file.

My XML looks like this:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><ResponseHeader xmlns="https://192.168.0.99/API"><Text>OK</Text></ResponseHeader></soap:Header><soap:Body><AccountResponse xmlns="https://192.168.0.99/API"><AccountResult>Available</AccountResult></AccountResponse></soap:Body></soap:Envelope>

and I am trying to read the value 'Available' from the AccountResult tag.

I have tried using the Xml2Map from: https://www.b4x.com/android/forum/t...ple-way-to-parse-xml-documents.74848/#content

The code I have tried is:

B4X:
Sub CheckAccount
    Dim xm As xml2map
    xm.Initialize
        
    Dim XML_value As String = $"<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><ResponseHeader xmlns="https://192.168.0.99/API"><Text>OK</Text></ResponseHeader></soap:Header><soap:Body><AccountResponse xmlns="https://192.168.0.99/API"><AccountResult>Available</AccountResult></AccountResponse></soap:Body></soap:Envelope>"$
    Dim phrasedata As Map
    phrasedata = xm.Parse(XML_value)
    Log(phrasedata.get("AccountResult"))
End Sub

However, it seems to return null

Any ideas on where I have gone wrong ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
hi!

there are a lot of steps to arrive to AccountResult

B4X:
 Dim envolepe As Map = xm.Parse(XML_value)

 dim body as map = envelope.get("soap:body")
 dim accResp as map = body.get("AccountResponse")
 dim result as map = accResp.get("AccountResult")
 log(result.get("text"))

double check my response, i did it without verification that it works.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
B4X:
Dim envolepe As Map = xm.Parse(XML_value)

 dim body as map = envelope.get("soap:body")
 dim accResp as map = body.get("AccountResponse")
 dim result as map = accResp.get("AccountResult")
 log(result.get("text"))
double check my response
Thanks heaps!

I thought I was missing something.

I got it to work with the following code:
B4X:
Dim xm As xml2map
    xm.Initialize
        
    Dim XML_value As String = $"<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><ResponseHeader xmlns="https://192.168.0.99/API"><Text>OK</Text></ResponseHeader></soap:Header><soap:Body><AccountResponse xmlns="https://192.168.0.99/API"><AccountResult>Available</AccountResult></AccountResponse></soap:Body></soap:Envelope>"$
    Dim envolepe As Map =  xm.Parse(XML_value)
    Dim Soapenvolepe As Map =  envolepe.get("soap:Envelope")
    Dim body As Map = Soapenvolepe.get("soap:Body")
    Dim accResp As Map = body.get("AccountResponse")
    
    Log("AccountResult = " & accResp.Get("AccountResult"))
 
Upvote 0
Top