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解析它,并且中文可以正常显示。谢谢!我解析完,中文都是乱码

解决方案 »

  1.   

    这是编码问题! 先将读取的xml字符串转换utf-8编码,再解析!比如:String strXML为获取的xml字符串byte[] datas=strXML.getBytes();
    String strXMLUtf=new String(datas,"utf-8");再解析strXMLUtf试试吧!
      

  2.   

    strXML是从如何来呢?首页你这就不对了吧
      

  3.   

    先获得文件的输入流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;
    }
      

  4.   

    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即可 试试
      

  5.   

    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子类中实现相应的接口方法即可。。
      

  6.   

    File file=new File("\mnt\sdcard\item.xml");
    FileInputStream fis=new FileInputStream(file);
      

  7.   

    在 Android 上使用 XML
    http://www.ibm.com/developerworks/cn/xml/x-android/这篇文章很不错