读取XML的问题

利用SAX读取写了代码,调试了一下午却一直在parse里抛异常。尼玛,Java的库函数还真心不怎么好用。

把代码贴上来先:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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,显示结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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;
        }
    }
 
}

  然后主窗口布局:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<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>

  显示结果窗口布局:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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>

 主窗口显示:

posted @   hailong  阅读(357)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2011-10-17 从txt读取数据到Cvmat
点击右上角即可分享
微信分享提示