0

I am making a simple android application and I am having issues parsing a simple XML file. I get the FileNotFoundException even through the file exists and I have all the permissions for it. I tried to modify the path in all the possible ways but I always get this exception thrown.

here's my code in the main activity:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Engine engine = new Engine();
engine.parse();

}

in the engine class:

 public void parse() {
            try {

                File fXmlFile = new File("/res/XML/users.xml");
                if (!fXmlFile.exists()) {
                    throw new FileNotFoundException("Failed to find file: " + 
                            fXmlFile.getAbsolutePath());
                    }
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
    ...

it really looks simple but I can't get this to work. any idea of what I'm doing wrong?

3
  • So did I understand correctly, the file is not found? I'm just wondering can you point to files outside of the emulator like you're trying to - or should the xml file reside within emulator file structure instead. Commented May 5, 2013 at 14:11
  • where can you see /Users/jon___davis/Desktop/users.xml on your device or simulator? Commented May 5, 2013 at 14:12
  • pasted the wrong string. the file is in res/XML but the situation doesn't change.. Commented May 5, 2013 at 14:16

1 Answer 1

2

In order to get a file out of the Resources you can use one of two calls.

Check out. http://developer.android.com/reference/android/content/res/Resources.html

You can use context.getResources().openRawResource(R.xml.users)

This will return an input stream allowing you to read the file.

Alternatively you can call context.getResources().getXml(R.xml.users)

This will return an XML Resource parser, this is a pullParser that will allow you to parse the xml.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.