B4J Question Parse SOAP/XML Reply

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I have used the HttpJob library to send a SOAP message when I send the request I get a XML reply in the JobDone Sub.

I have made it log the reply using the following code:
B4X:
Public Sub JobDone (Job As HttpJob)
    If Job.Success = True Then
        Job.GetString
    End If
End Sub

The file it logs is as follows:

HTML:
<?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://URL_HERE"><Text>3 account(s) found.</Text></ResponseHeader></soap:Header><soap:Body><ListAccountsResponse xmlns="https://URL_HERE"><ListAccountsResult><string>12345678,abc,dfg</string><string>1234,test,testing</string><string>abc123,demo,testing</string></ListAccountsResult></ListAccountsResponse></soap:Body></soap:Envelope>

Now I am trying to work out how to parse the XML reply so I can read the string values in the XML file.

I am trying to log:
12345678,abc,dfg
1234,test,testing
abc123,demo,testing

Any idea how I can parse the XML to get these values ?
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

one way is by using the library jXmlSax (this is a B4J standard library).

XML File
For testing have stored your xml string in a file soapresult.xml in the dirapp folder.
B4X:
<?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://URL_HERE">
        <Text>3 account(s) found.</Text>
    </ResponseHeader>
</soap:Header>
<soap:Body>
    <ListAccountsResponse xmlns="https://URL_HERE">
        <ListAccountsResult>
            <string>12345678,abc,dfg</string>
            <string>1234,test,testing</string>
            <string>abc123,demo,testing</string>
        </ListAccountsResult>
    </ListAccountsResponse>
</soap:Body>
</soap:Envelope>

Sample B4J Code tested
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private XMLParser As SaxParser
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    XMLParser.Initialize
    ParseSoapResult
    MainForm.Close
End Sub

Sub ParseSoapResult
    Dim soapresult As String = File.ReadString(File.DirApp, "soapresult.xml")
    XMLParser.parse(StringToInputStream (soapresult), "Parser")
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    Select Name
        Case "string"
            Log(Text.ToString)
    End Select
End Sub

Sub StringToInputStream (s As String) As InputStream
   Dim In As InputStream
   Dim data() As Byte = s.GetBytes("UTF8")
   In.InitializeFromBytesArray(data, 0, data.Length)
   Return In
End Sub

Output in Log
12345678,abc,dfg
1234,test,testing
abc123,demo,testing
 
Upvote 0
Top