How to parse various XML files using SAX on Android?

I have the following scenario.

I send the XML file to the server as a request and get the XML file as a response and all this as a background thread on Android.

An XML request is serialized using various values. The XML response is then read by SAX and placed on the list. The entire request / response process takes place in the background thread using the class AsyncTask.

The problem is that I have different types of answers, and I have no idea what approach to take to analyze the answer based on the sent request.

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

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

+1
source share
2 answers

Here are the options I see:

  • You can base your parsing capabilities on the basis of a query (which should have some kind of context). If you know that the request is type A, then you know that the answer MUST use parser A.
  • You can have a common parser for all types and branch out what happens in the parser based on the first known tag or attribute that dictates what needs to be done.
  • , . . , , ( xml - , , ).

!

+2

, xml

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

<response>
<error/>
<data type="B">
</data>
</response>
+1

Source: https://habr.com/ru/post/1762423/


All Articles