0

Possible Duplicate:
Parsing local xml file in android

Hi every one, In this folowing link there is a xml parser

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

instead of using url can i use the xml file stored locally, means inside "res/xml/*.xml" can any one provide me a code for dat...(am brand new to android, pls help me)

Community
  • 1
  • 1
Sowmya
  • 693
  • 3
  • 9
  • 19

3 Answers3

1

try using assest folder like this

    AssetManager assetManager = getAssets();
    InputStream stream = null;

    try {

        stream = assetManager.open("sample.xml");

    } catch (IOException e) {

        // handle

    }

and use this

xr.parse(stream);

stream object instead of

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

One thing more: download this file and use above code put this file in ur assest folder

ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
  • thanx for ur reply,if u hav any similar sample code ill u give me zip file of the same,,, – Sowmya Jan 19 '11 at 12:52
  • I had done xml parsing but mine requirements are different from yours its better you spend some time with link that you posted and rest what you have to do is already mentioned by me – ingsaurabh Jan 19 '11 at 12:58
  • @Sowmya: stop buzzing around asking for code! Saurabh also gave you the same suggestion as I did. try to work it out yourself first. if you're stuck at some points, you can always come back here with your exact problem and ask. – springrolls Jan 19 '11 at 13:08
  • thank u saurabh,, finally i solved my problem from u,,keep suggesting – Sowmya Jan 20 '11 at 12:15
  • anytime, one thing more if u got the answer mark the post as answered by clicking correct tick – ingsaurabh Jan 20 '11 at 13:03
0

Refer this Question Parsing local XML file using Sax in Android

Community
  • 1
  • 1
Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90
0
private void parseData()
    {
         SAXParserFactory spf = SAXParserFactory.newInstance();
         SAXParser sp = null;
        try {
            sp = spf.newSAXParser();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

         // Get the XMLReader of the SAXParser we created.
         XMLReader xr = null;
        try {
            xr = sp.getXMLReader();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         // Create a new ContentHandler and apply it to the XML-Reader
        OfficesParser myExampleHandler = new OfficesParser();
         xr.setContentHandler(myExampleHandler);

         // Parse the xml-data from our URL. 
         try {



                        File f = new File("MyLocal.xml");
            xr.parse(new InputSource(new URL(f.toURL()).openStream()));

        } catch (MalformedURLException e) {
            //Log.d("Net Disconnected", "NetDisconeeted");
            // Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
            // TODO Auto-generated catch block
            //e.printStackTrace();
        } catch (IOException e) {
            //Log.d("Net Disconnected", "NetDisconeeted");
            // Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
            // TODO Auto-generated catch block
            //e.printStackTrace();
        } catch (SAXException e) {
            //Log.d("Net Disconnected", "NetDisconeeted");
            // TODO Auto-generated catch block
            //e.printStackTrace();
        }
         catch (Exception e) {
                //Log.d("Net Disconnected", "NetDisconeeted");
                // TODO Auto-generated catch block
                //e.printStackTrace();
            }
    }
Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72
  • thanx for ur reply,,i tried ur code but i didnt get out put,,if u hav any similar code ill u pls give me zip file of that project,,thanx in advance – Sowmya Jan 19 '11 at 12:32