如何在java / android中用sax解析XML? [英] How to parse XML with sax in java / android ?

查看:55
本文介绍了如何在java / android中用sax解析XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的方法来解析XML文件

但我的XML文件是UFT-8

这是我的Erro:意外的令牌(位置:TEXTï» ¿@ 1:4 in java.io.StringReader@529f6e70)





和我的解析XML代码:

I am using below method to parse an XML file
But My XML file is UFT-8
This is My Erro : Unexpected token (position:TEXT @1:4 in java.io.StringReader@529f6e70)


and My code for parse XML :

public Document getDomElement(String xml) {
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setEncoding("ISO-8859-1"); //UTF-8 or 16
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);
            //doc = db.parse(new ByteArrayInputStream(xml.getBytes()));

        }
        catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }
        catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }
        catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }
        return doc;
    }

推荐答案

这里有一些你可以遵循的好教程:



http://www.javatpoint.com/android-xml-parsing-using- sax-parser [ ^ ]





http://www.pcsalt.com/android/xml-parsing-using-saxparser-android/ [ ^ ]
Here Is Some Good Tutorial You Can Follow:

http://www.javatpoint.com/android-xml-parsing-using-sax-parser[^]


http://www.pcsalt.com/android/xml-parsing-using-saxparser-android/[^]


这篇关于如何在java / android中用sax解析XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆