利用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 ArrayAdapter<String> adapter; @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<String>(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 HashMap<String, String> m_AyaContentMap; private Map<String, String> m_SuarIDSet; private HashMap<String, String> m_Id2IDsHashMap; public HashMap<String, String> getAyaContentMap(){ return m_AyaContentMap; } public Map<String, String> getSuarIDSet(){ return m_SuarIDSet; } public HashMap<String, String> 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; } } }
然后主窗口布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/keywordLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关键字查询:" android:paddingTop="15px" /> <EditText android:id="@+id/keywordEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/keywordLabel" android:layout_alignBaseline="@id/keywordLabel" /> <Button android:id="@+id/findKeywordButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/keywordEditText" android:layout_alignRight="@id/keywordEditText" android:text="查找" /> <TextView android:id="@+id/ChapterTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="章节查询:" android:layout_below="@id/findKeywordButton" android:paddingTop="15px" /> <TextView android:id="@+id/SuraIDText" android:text="章:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ChapterTextView" /> <Spinner android:id="@+id/SuraIDSpinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/SuraIDText" android:paddingTop="15px" /> <TextView android:id="@+id/AyaIDText" android:text="节:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/SuraIDSpinner" /> <Spinner android:id="@+id/AyaIDSpinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/AyaIDText" /> <Button android:id="@+id/findChapterButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/AyaIDSpinner" android:layout_alignRight="@id/AyaIDSpinner" android:text="查找" android:onClick="onClickSuraAyaFindBtn" /> </RelativeLayout>
显示结果窗口布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".FindResActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/FindResShow" /> <TextView android:id="@+id/ResText" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
主窗口显示: