如何在Android上使用SAX解析不同的XML文件? [英] How to parse different XML files using SAX on Android?

查看:137
本文介绍了如何在Android上使用SAX解析不同的XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情形。

我一个XML文件发送到服务器的请求,并得到一个XML文件作为响应和所有为Android上的后台线程。

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.

XML请求是使用各种数值序列化。 XML响应然后通过SAX读取并放入列表。整个请求/响应过程中使用的AsyncTask 类发生在后台线程。

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.

我如何基于请求B告诉Android的使用基于请求A和解析器乙解析器的?

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

编辑:与不同类型的反应我的意思是XML文件看起来不同。它有不同的标签,不同的属性。

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

推荐答案

下面是选项,我看到他们:

Here are the options as I see them:


  1. 您可以依据基于请求(它应该有某种语境)解析能力。如果您知道请求的是A型,那么你知道的反应必须使用解析器。

  2. 您可以有通用的解析器的所有类型,和分支什么的发生的基于该决定应该做什么第一个已知的标签或属性解析器。

  3. 解析首先,分析之后。把所有的反应元件,并建立对象或对象图了出来。通过周围的人,他们的变异如果需要的话您的应用程序(不要尝试编辑XML - 你解析它毕竟和你想,要尽快完成)

  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).

祝你好运!

这篇关于如何在Android上使用SAX解析不同的XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆