0

Before posting this question i had search all the places to get some similar question, I had found questions but no answer worked for me, I used getResources, that approach also gave me a error saying Undefined Method, using this or Context also dint work, I just need to read and write a xml in android app and the xml should be stored in my app in res folder or any other folder which is standard, Right now i am using the below code, Please help me with this. Thanks a lot ..Max

                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

                Document doc = docBuilder.parse (new File("savecredentials.xml"));

Using getResources doesnt help, How to use getResources when it is undefined, and if i use context it doesnt work , Any simple solution for this one.

max
  • 3,047
  • 5
  • 20
  • 17

1 Answers1

0

You had write new File("savecredentials.xml") which means you are reading savecredentials.xml from application folder.You had not set path of res folder. You have to mentioned path from where you are reading file.

For example if you had a file in asset folder then you can write

    AssetManager mg = getResources().getAssets();
            InputStream myInput = null;
            try {
                myInput = mg.open("File name");
                if(myInput != null) {
                    System.out.println("File exists in assets");
                }
            } catch (IOException ex) {
                System.out.println("File not exists in assets");

            }

XmlPullParserFactory factory = null;
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = null;
xpp = factory.newPullParser();
InputStreamReader reader = new InputStreamReader(myInput);
xpp.setInput(reader);
int eventType = 0;
eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    if(eventType == XmlPullParser.START_TAG) {

    } else if(eventType == XmlPullParser.END_TAG) {

    } else if(eventType == XmlPullParser.TEXT) {
    }
    eventType = xpp.next();
}

This code is for check weather file is exist in asset or not ?

And read from res folder

Click here

Community
  • 1
  • 1
Dharmendra
  • 33,296
  • 22
  • 86
  • 129
  • what is c in the above context, Also i dont have a database to open I have a xml file. and then i need to use the Document doc = docBuilder.parse (new File("savecredentials.xml")); , can you suggest me something in the above context. – max Aug 29 '11 at 06:38
  • getResouces method is undefined, Using Context or this also doesnt work. – max Aug 29 '11 at 06:54
  • please read my previous comment and the question i already mentioned that Context is not working. – max Aug 29 '11 at 07:05
  • getResources() method is for context class so you have to make object and then you have to call this method it will worked for you – Dharmendra Aug 29 '11 at 07:08
  • not working please mention what to do with the context, is it Context c = new Context(); – max Aug 29 '11 at 09:22
  • Are you writing this code in Activity, Service or any where else ? – Dharmendra Aug 29 '11 at 09:27
  • not in the activity class, its in a class which is written just to handle xml read and write. – max Aug 29 '11 at 09:49
  • pass the reference of your Activity to that class and using that activity reference call getResources() method in that class. – user370305 Aug 29 '11 at 10:50