利用SAX读取写了代码,调试了一下午却一直在parse里抛异常。尼玛,Java的库函数还真心不怎么好用。
把代码贴上来先:
package com.example.gulanfinddemo;import java.io.IOException;import javax.xml.parsers.ParserConfigurationException;import org.xml.sax.SAXException;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Spinner;public class MainActivity extends Activity { private Spinner suraIDpinner; private QuranXmlParser quranXmlParser; final public static String ExtraStr = "FindRes"; private static final String[] m={"1","2","3","4","5"}; private ArrayAdapteradapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); quranXmlParser = new QuranXmlParser(); Log.v("MainActivity", "1"); try { quranXmlParser.ReadXML("GuLan.xml"); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } InitSuraIDSpinner(); suraIDpinner.setPrompt("请输入将要查询的章树"); //设置下拉列表的风格 adapter = new ArrayAdapter (this, android.R.layout.simple_spinner_dropdown_item,m); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //将adapter 添加到spinner中 suraIDpinner.setAdapter(adapter); } public void InitSuraIDSpinner(){ suraIDpinner = (Spinner)findViewById(R.id.SuraIDSpinner); } public void onClickSuraAyaFindBtn(View view){ Intent intent = new Intent(this, FindResActivity.class); String selectedString = suraIDpinner.getSelectedItem().toString(); if(null==selectedString){ selectedString="NoThing"; } intent.putExtra(ExtraStr, selectedString); startActivity(intent); }}
新建一个FindResActivity,显示结果:
package com.example.gulanfinddemo;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.widget.TextView;public class FindResActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_find_res); Intent intent = getIntent(); String findReString = intent.getStringExtra(MainActivity.ExtraStr); TextView resText = (TextView)findViewById(R.id.ResText); resText.setText(findReString); } }
一个QuranXmlParser类:
package com.example.gulanfinddemo;import java.io.IOException;import java.io.StringReader;import java.util.HashMap;import java.util.Map;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;import android.util.Log;public class QuranXmlParser{private String m_res;private volatile boolean m_isDoFind;public QuranXmlParser(){ m_isDoFind=false;}public boolean ReadXML(String xmlString) throws ParserConfigurationException, SAXException, IOException{ Log.v("ReadXML", "1"); XMLHandler handler = new XMLHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); parser.parse(xmlString,handler); Log.v("ReadXML", "2"); return true;}}
新建一个XMLHandler,负责具体的读取XML:
package com.example.gulanfinddemo;import java.io.IOException;import java.io.StringReader;import java.util.HashMap;import java.util.Map;import java.util.jar.Attributes;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;import android.R.integer;import android.util.Log;import android.util.Pair;public class XMLHandler extends DefaultHandler{ public static String tag = "XMLHandler"; final int quran = 0; final int suraID = 1; final int suraName = 2; final int aya = 3; final int qurantext = 4; int currentState = 0; String suraIDString; String suraNameString; String ayaID; private HashMapm_AyaContentMap; private Map m_SuarIDSet; private HashMap m_Id2IDsHashMap; public HashMap getAyaContentMap(){ return m_AyaContentMap; } public Map getSuarIDSet(){ return m_SuarIDSet; } public HashMap getId2IDsHashMap(){ return m_Id2IDsHashMap; } public void startElement(String namespacesURI , String localName , String qName , Attributes atts) throws SAXException{ if(localName.equals("sura")){ if(qName.equals("suraID")){ currentState = suraID; } else if(qName.equals("suraName")){ currentState = suraName; } } else if(localName.equals("aya")){ currentState = aya; } else if (localName.equals("qurantext")){ currentState = qurantext; } else if(localName.equals("quran")){ currentState = quran; } } public void endElement(String namespacesURI , String localName , String qName) throws SAXException{ } public void characters(char[] ch , int start , int length) throws SAXException{ String theString = new String(ch, start, length); if(null != theString){ } switch(currentState){ case suraID:{ suraIDString = theString; } break; case suraName:{ suraNameString = theString; m_SuarIDSet.put(suraIDString, suraNameString); } case aya:{ ayaID = theString; } break; case qurantext:{ m_Id2IDsHashMap.put(suraIDString,ayaID); final String keyString = suraIDString+":"+ayaID; m_AyaContentMap.put(keyString, theString); Log.v(tag,keyString+" "+theString); } break; } }}
然后主窗口布局:
显示结果窗口布局:
主窗口显示: