Antonis
Antonis

Reputation: 1061

Formatted string parsing

I'm calling a web service that returns the following string:

anyType{x=4;y=5;z=acq}

How do you get the values of x, y and z?

Upvotes: 0

Views: 1307

Answers (2)

Antonis
Antonis

Reputation: 1061

Regarding the web service, the problem is with the KSOAP2 android library that i' m using. the code i used to get the result was:

HttpTransportSE ht = new HttpTransportSE(URL);
ht.debug = true;
ht.call(SOAP_ACTION, envelope);
String response = envelope.getResponse().toString();

I had to use the following line to get the response as xml:

String responseXML = ht.responseDump;

So now there is no problem with parsing the xml response. Thank you all for your help.

Upvotes: 0

whirlwin
whirlwin

Reputation: 16521

This is not an optimal solution, but works for debugging info.

However, it is, like others suggested preferable to get the result as XML (or JSON), as you can use a robust library for parsing the data.

final Matcher matcher =
        Pattern.compile("\\w+\\{x=(\\w+);y=(\\w+);z=(\\w+)\\}")
               .matcher("anyType{x=4;y=5;z=acq}");

while (matcher.find()) {
    final String x = matcher.group(1);
    final String y = matcher.group(2);
    final String z = matcher.group(3);
}

Upvotes: 4

Related Questions