博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读取XML的问题
阅读量:6036 次
发布时间:2019-06-20

本文共 6103 字,大约阅读时间需要 20 分钟。

利用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
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
(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
m_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; } }}

  然后主窗口布局:

  显示结果窗口布局:

 主窗口显示:

转载地址:http://bjohx.baihongyu.com/

你可能感兴趣的文章
Codis集群的搭建与使用
查看>>
织梦dedecms安全设置详情
查看>>
Delphi常用关键字用法详解
查看>>
负面话语的危害
查看>>
Android 技巧 - Debug 判断不再用 BuildConfig
查看>>
算法笔记_224:夺冠概率模拟(Java)
查看>>
整型数字按位取
查看>>
2015百度之星初赛2 1005 序列变换(LIS变形)
查看>>
USACO 5.1.1凸包
查看>>
sql assist字符匹配智能提示
查看>>
tcMalloc 配置和优化 nginx 高性能
查看>>
P1186 玛丽卡
查看>>
Mac下Homebrew的图形化界面工具Cakebrew
查看>>
数据库存储 层次、树形结构 的标准做法
查看>>
[转] 一个小时学会Git
查看>>
maven资源文件的相关配置
查看>>
django学习2 视图和模板
查看>>
soap的调用方式
查看>>
IDEA 运行maven命令时报错: -Dmaven.multiModuleProjectDirectory system propery is not set
查看>>
01Hadoop二次排序
查看>>