What is commonly done is to create a BufferedReader
to store the value of the answer,
<?xml version="1.0" encoding="utf-8"?><boolean xmlns="http://pda.gim/">true</boolean>
From this answer, which is an xml, a parsing is performed to obtain the value of the "boolean" tag, when determining its value you can use the logic you want in your program:
StringBuilder sb = new StringBuilder();
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(entity.getContent()));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
//En base a respuesta realiza parsing para obtener el valor del tag boolean.
if (parseXml(sb.toString(), "boolean").equals("true")){
//Contiene valor true.
}else{
//No contiene valor true, por lo tanto es false.
}
}catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
This is a method used in the previous code, in which the response and the name of the tag are sent (in this case "boolean") and we obtain its value:
public String parseXml(String xml, String tag){
try {
boolean obtieneValor = false;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput( new StringReader(xml));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
//Log.d(TAG,"Start document");
} else if(eventType == XmlPullParser.START_TAG) {
//Log.d(TAG,"Start tag "+xpp.getName());
if(xpp.getName().equals(tag)) {
obtieneValor = true;
}
} else if(eventType == XmlPullParser.END_TAG) {
//Log.d(TAG,"End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
//Log.d(TAG,"valor Text "+xpp.getText());
if(obtieneValor && xpp.getText()!= null){
return xpp.getText();
}
obtieneValor = false;
}
eventType = xpp.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
This method can be useful to determine the value of any tag in your xml response.
String valor = parseXml(<respuesta XML>, <nombre de tag>);