0

I have an XML file in the drawable-folder. I want parse the XML-file using XML-parsing(sax parsing).
I used following code for that:

private void getDataFromFile(int mntFile) throws SAXException, IOException {
        // TODO Auto-generated method stub
        SAXParserFactory factory = SAXParserFactory.newInstance();
        System.out.println("in method");
        try {
            saxParser = factory.newSAXParser();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        DefaultHandler handler = new DefaultHandler() {

        boolean id = false;
        boolean name = false;
        boolean dep = false;


        public void startElement(String uri, String localName,String qName, 
                    Attributes attributes) throws SAXException {
            //System.out.println("in start");
            if (localName.equals("DOCTORID")) {
                id = true;
            }

            if (localName.equals("NAME")) {
                name = true;
            }

            if (localName.equals("DEAPRTMENT")) {
                dep = true;
            } 
        }

        public void endElement(String uri, String localName,
            String qName) throws SAXException { 
        }

        public void characters(char ch[], int start, int length) throws SAXException {

            if (id) {
                id = false;
            }

            if (name) {
                System.out.println("Name : " + new String(ch, start, length));
                mArryLstDoctorNames.add(new String(ch, start, length));
                Log.d("doctor","---"+mArryLstDoctorNames);
                name = false;
            }

            if (dep) {
                dep = false;
            }

        }

         };
           saxParser.parse(R.drawable.myxml, handler);
    }

But here saxParser.parse(R.drawable.myxml, handler); is showing compilation error.
It is the same code working for XML-files, which are on sdcard.

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
SuReSh PaTi
  • 1,373
  • 7
  • 28
  • 46

1 Answers1

1

You can't store it to drawable folder, it is part of your package, store it elsewhere, yourpackage/data or SD card.
R.drawble.xml is not use a input source.

First you need to read the file, then pass it to a string, after that you can parse it.

This is how:

FileInputStream fis = openFileInput(yourfilepath);
        InputStreamReader isr = new InputStreamReader(fis);
        char[] inputBuffer = new char[reasonableLenght];
        // Fill the Buffer with data from the file
        isr.read(inputBuffer);
        String xml_data = new String(inputBuffer);
     //then you can do
saxParser.parse(new InputSource(new StringReader(xml_data)), handler);
Nickolaus
  • 4,785
  • 4
  • 38
  • 60
Sarim Sidd
  • 2,166
  • 2
  • 22
  • 31
  • my xml file in drawable .how to get file path? – SuReSh PaTi May 02 '12 at 16:14
  • you cant store it to drawable folder, its part of your package, store it elsewhere,yourpackage/data or SD card, and remember to tick the answer if it has helped you – Sarim Sidd May 02 '12 at 16:22
  • how to copy my file from assets to /data/data/packagesname/folder/ dynamically – SuReSh PaTi May 02 '12 at 17:13
  • 1
    this will help : http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard . and for further questions ask a new question our search yourself first,google it. – Sarim Sidd May 02 '12 at 18:30