file not found with DocumentBuilderFactory
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.
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
精彩评论