views:

109

answers:

2

Hey there,

I have the following scenario: I send a XML file to a server as a request and get a XML file as response and all that as a background thread on android.

The XML request is serialized using various values. The XML response is then read by SAX and put into a list. The whole request/response process happens in a background thread using the AsyncTask class.

The problem is that I have different types of responses and I have no idea what approach to take to parse the response based on the request sent.

How do I tell Android to use parser A based on request A and parser B based on request B?

Appreciate your help.

EDIT: With different types of responses I mean the XML file looks different. It has different tags and different attributes.

+1  A: 

the are all message response object, specify an attribute to indicate and object type in the xml response

<response>
<error/>
<data type="A">
</data>
</response>

<response>
<error/>
<data type="B">
</data>
</response>
Aaron Saunders
Only problem I have is that I can't modify the XML.
Octavian Damiean
then parse the xml for a specific differentiator and then branch the code on that value.
Aaron Saunders
+1  A: 

Here are the options as I see them:

  1. You can base the parsing capability based on the request (which should have some kind of context). If you know the request is Type A, then you know the response MUST use parser A.
  2. You can have generic parser for all types, and branch what happens in the parser based on the first known tag or attribute that dictates what should be done.
  3. Parse first, analyze after. Take all the response elements, and build object or object graphs out of them. Pass those around, mutate them if need be for your application (don't try to edit xml - you're parsing it after all, and you want that to finish ASAP).

Good luck!

Nate
Thanks for your answer. Your last option seems pretty interesting. Is there a way to achieve this using the SAX parser?
Octavian Damiean
Sure, any start/end element event can be used to delineate object boundaries.
Nate