说在前面
熟悉安卓开发的都知道我们传输数据时可以使用XML或者JSON形式来传递数据。
先说说我对他们优缺点的看法:
就XML来说,优点是更加直观,容易一眼就看出整体结构,父与子层次分明,不会担心认错了爹和兄弟。缺点也很明显,父子节点多了之后也很不清晰,还有就是没有直接的提取工具,需要手动,手动,手动去解析([○・`Д´・ ○])!!!这就导致了每次改动数据结构就不只要重新修改bean,还要修改提取工具。
而JSON,虽然你拿到后台给你的文档时你一脸懵逼,哭着数到底有多少同级的标签,又有多少子标签,数的时候心塞塞的。但是当真正搞清楚之后开始写业务逻辑,那就丝般顺滑,因为Java提供了JSON解析类JSONObject和JSONArray,使用起来so easy,而且改动数据(实体类)的时候也游刃有余,这就很舒服有没有!
重点来了
先说说怎么用吧,主要使用流程是下面这段代码:
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
InputStream inputStream;
inputStream = getAssetManager().open("action/actionlist1.xml");
ActionListSax handle = new ActionListSax();
saxParser.parse(inputStream,handle);
inputStream.close();
首先通过SaxParserFactory来获取一个新的实例,返回的是一个继承SaxParserFactory抽象类的对象SaxParserFactoryImpl,
然后第二行调用newSaxParser方法获得到一个继承SaxParser抽象类的对象SaxParserImpl
SAXParserImpl(Map<String, Boolean> initialFeatures)
throws SAXNotRecognizedException, SAXNotSupportedException {
this.initialFeatures = initialFeatures.isEmpty()
? Collections.<String, Boolean>emptyMap()
: new HashMap<String, Boolean>(initialFeatures);
resetInternal();
}
private void resetInternal()
throws SAXNotSupportedException, SAXNotRecognizedException {
reader = new ExpatReader();
for (Map.Entry<String,Boolean> entry : initialFeatures.entrySet()) {
reader.setFeature(entry.getKey(), entry.getValue());
}
}
这里传进来的initialFeatures是empty的,我们看到在构造函数中还有一个resetInternal函数,新建了一个ExpatReader对象并传递引用给reader。之后我们调用了这个SaxParser的parse函数,将inputstream和handle传入,在SaxParser抽象类源码中我们看到了,调用的方法其实是:
public void parse(InputStream is, DefaultHandler dh)
throws SAXException, IOException {
if (is == null) {
throw new IllegalArgumentException("InputStream cannot be null");
}
InputSource input = new InputSource(is);
this.parse(input, dh);
}
最后一行this.parse(input,dh)调用了下面的方法:
public void parse(InputSource is, DefaultHandler dh)
throws SAXException, IOException {
if (is == null) {
throw new IllegalArgumentException("InputSource cannot be null");
}
XMLReader reader = this.getXMLReader();
if (dh != null) {
reader.setContentHandler(dh);
reader.setEntityResolver(dh);
reader.setErrorHandler(dh);
reader.setDTDHandler(dh);
}
reader.parse(is);
}
这里调用了方法getXMLReader方法, 是个抽象方法,那么我们去找找它的实现。
@Override
public XMLReader getXMLReader() {
return reader;
}
在SaxParserImpl中找到了实现,那么这个reader我们在上面的resetInternal方法中初始化了,是ExpatReader对象,我们将我们实现了contentHandler,entityResolver,errorHandler,dthHandler接口的DefaultHandler传了进去,之后调用了ExpatReader对象的parse方法,来看看它干了什么:
public void parse(InputSource input) throws IOException, SAXException {
if (processNamespacePrefixes && processNamespaces) {
/*
* Expat has XML_SetReturnNSTriplet, but that still doesn't
* include xmlns attributes like this feature requires. We may
* have to implement namespace processing ourselves if we want
* this (not too difficult). We obviously "support" namespace
* prefixes if namespaces are disabled.
*/
throw new SAXNotSupportedException("The 'namespace-prefix' " +
"feature is not supported while the 'namespaces' " +
"feature is enabled.");
}
// Try the character stream.
Reader reader = input.getCharacterStream();
if (reader != null) {
try {
pa