android SAX解析XML文件

fish007fish2003 2011-07-04 03:35:35
xml文件,存放路径为“\mnt\sdcard\item.xml”
<?xml version="1.0" encoding="UTF-8"?>
<paper>
<question1>
<name>文字填空</name>
<type>1</type>
<option>
<count>3</count>
<option1>测试1</option1>
<option2>测试2</option2>
</option>
</question1>
</paper>
该如何使用SAX解析它,并且中文可以正常显示。谢谢!我解析完,中文都是乱码
...全文
512 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruiminghaohao 2011-08-03
  • 打赏
  • 举报
回复
能具体详细点吗?我也正在学习!嘿嘿···
skyzhg 2011-07-05
  • 打赏
  • 举报
回复
正在用,学习了
多鱼的夏天 2011-07-05
  • 打赏
  • 举报
回复
在 Android 上使用 XML
http://www.ibm.com/developerworks/cn/xml/x-android/

这篇文章很不错
傲慢的上校 2011-07-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wenzheng38 的回复:]

import org.xml.parser.*;

SAXParserFactory spf = SAXParserFactory.getInstance();
SAXPaeser saxParser = spf.newSAXParser(); //创建解析器
XMLContentHander handler = new XMLContentHander(); //……
[/Quote]ok?
lizhengjun2010 2011-07-04
  • 打赏
  • 举报
回复
还是不行的话!加QQ:465144122
思考的芦苇 2011-07-04
  • 打赏
  • 举报
回复
File file=new File("\mnt\sdcard\item.xml");
FileInputStream fis=new FileInputStream(file);
[Quote=引用 5 楼 wenzheng38 的回复:]

import org.xml.parser.*;

SAXParserFactory spf = SAXParserFactory.getInstance();
SAXPaeser saxParser = spf.newSAXParser(); //创建解析器
XMLContentHander handler = new XMLContentHander(); //……
[/Quote]
wenzheng38 2011-07-04
  • 打赏
  • 举报
回复
import org.xml.parser.*;

SAXParserFactory spf = SAXParserFactory.getInstance();
SAXPaeser saxParser = spf.newSAXParser(); //创建解析器
XMLContentHander handler = new XMLContentHander(); //继承了DefaultHandler(DefaultHandler是对ContentHander接口的简单实现)
saxParser.parser(inputStream, handler);

XMLContentHander需要实现ContentHandler接口的startDocument、endDocument、startElemetn、endElement、characters等基本方法。

LZ在自己定义的ContentHander子类中实现相应的接口方法即可。。。。。
lizhengjun2010 2011-07-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 aspirehouse 的回复:]

引用 1 楼 lizhengjun2010 的回复:

这是编码问题! 先将读取的xml字符串转换utf-8编码,再解析!

比如:String strXML为获取的xml字符串

byte[] datas=strXML.getBytes();
String strXMLUtf=new String(datas,"utf-8");

再解析strXMLUtf

试试吧!
……
[/Quote]


File file=new File("\mnt\sdcard\item.xml");
FileInputStream fis=new FileInputStream(file);
byte[] datas=new byte[1024];
int len;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
while((len=fis.read(datas))!=0)
baos.write(datas, 0, len);
String strXML=new String(baos.toByteArray(),"utf-8");

解析strXML即可

试试
AspireHouse 2011-07-04
  • 打赏
  • 举报
回复
先获得文件的输入流FileInputStream fis;
再调用这个方法获得输入流is,传给sax解析器解析
private InputStream encode(InputStream fis)
{
InputStream is;
byte[] b = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try
{
while((len = fis.read(b)) != -1)
{
bos.write(b , 0 , len);
}
byte[] data = bos.toByteArray();
if(bos != null)
{
bos.close();
}
if(fis!= null)
{
fis.close();
}
try
{
is = new ByteArrayInputStream(new String(data , "utf-8");
}
catch (IOException e)
{
is = new ByteArrayInputStream(data);
}
}
catch (IOException e1)
{
is = fis;
}
return is;
}
AspireHouse 2011-07-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lizhengjun2010 的回复:]

这是编码问题! 先将读取的xml字符串转换utf-8编码,再解析!

比如:String strXML为获取的xml字符串

byte[] datas=strXML.getBytes();
String strXMLUtf=new String(datas,"utf-8");

再解析strXMLUtf

试试吧!
[/Quote]
strXML是从如何来呢?首页你这就不对了吧
lizhengjun2010 2011-07-04
  • 打赏
  • 举报
回复
这是编码问题! 先将读取的xml字符串转换utf-8编码,再解析!

比如:String strXML为获取的xml字符串

byte[] datas=strXML.getBytes();
String strXMLUtf=new String(datas,"utf-8");

再解析strXMLUtf

试试吧!

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧