0

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

Sowmya
  • 693
  • 3
  • 9
  • 19

4 Answers4

3

This is what I did,

InputStream is = res.openRawResource(R.raw.fileName);
xr.parse(new InputSource(is));
springrolls
  • 1,331
  • 1
  • 14
  • 40
  • thanx for ur reply theresia, but wat is "res"? am getting error res not declared,have u tried for that proj which is in that link,is that executing? – Sowmya Jan 19 '11 at 04:09
  • Resource res = getResources(); Your class has to extend Activity, or at least get access to it. Pass it as a parameter, if you put your code in another class that doesn't extend Activity. I haven't tried the IBM tutorial, but I take some essential code from there. It should work for you, too. – springrolls Jan 19 '11 at 08:30
  • thank u,,,,can u please give me zip file of parsing local xml (with repitative tags) sample project – Sowmya Jan 19 '11 at 08:41
  • do you have any other problems with the repetitive tags? you can always ask. and i thought i read one post about the same question here. good luck! – springrolls Jan 19 '11 at 08:52
  • no still am unable to do that,,if u hav any example please ill give me – Sowmya Jan 19 '11 at 11:53
  • you're asking different question. better post a new one. explain what you've done, what's the problem, and give the error trace if any. – springrolls Jan 19 '11 at 12:43
  • @Sowmya you can refer this link for perse xml from local rea/raw folder http://android-vogue.blogspot.com/search/label/xml%20perse%20local%20from%20res%2Fraw – Herry Jun 03 '11 at 05:16
1
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);
        }
iAndroid
  • 951
  • 7
  • 18
1

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

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
0

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

Jonathan Roth
  • 1,271
  • 1
  • 12
  • 19