Parsing local xml file in android

2019-03-03 22:41发布

Hii every one, am brand new to android,i have a doubt can any one help me

In this following link

http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/

there is a sax parser in which xml file is taken from the internet, using a URL path How to change that url to local path in which xml is stored in raw folder of the project,,can any one give me the syntax ,,thanx in advance

4条回答
Explosion°爆炸
2楼-- · 2019-03-03 23:04

In the link you provided, replace the line 47

xr.parse(new InputSource(sourceUrl.openStream()));

with

xr.parse(getResources().getAssets().open(fileName));

and place your xml file in /res/raw folder

links: Asset Manager Docs and Resources Manager Docs

查看更多
乱世女痞
3楼-- · 2019-03-03 23:05
try {
//          InputStream is = getResources().getAssets().open("yourfilename.xml");
            InputStream is =getAssets().open("yourfilename.xml");
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            MyXMLHandler myXMLHandler = new MyXMLHandler();
            xr.setContentHandler(myXMLHandler);

            xr.parse(new InputSource(is));

        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }
查看更多
Explosion°爆炸
4楼-- · 2019-03-03 23:13

This is what I did,

InputStream is = res.openRawResource(R.raw.fileName);
xr.parse(new InputSource(is));
查看更多
不美不萌又怎样
5楼-- · 2019-03-03 23:14

In Android you can store data in the assets folder, which can be entered by your code. To address your file use

file:///android_asset/yourFile.xml

I haven´t tried this yet, but I hope it will work

查看更多
登录 后发表回答