I'm trying to read an xml file which I plan to include as part of my mobile application. I've created a folder called "XML" and added a file called "test.xml". The files build action is set to content. When I run the app it crashes on trying to find the file. Here is the code I'm using:
_path = "XML/test.xml";
doc = new XmlDocument();
doc.Load(_path);
Here is the error I get:
System.IO.DirectoryNotFoundException has been thrown.
Could not find a part of the path "/XML/test.xml"
Do I need to register the directory/folder somehow? Thanks for any advice, I'm sure I've just missed something simple...
Do you mean that you'd created XML directory inside of your project?
If you want to include XML to your APK - one of the best ways will be to create /res/xml/file.xml and read it inside application with Resources class.
Please have a look at this thread - maybe you'll find it helpful. Open XML file from res/xml in Android
Good luck
Related
I have a desktop application that produces resource / data files for my android app. These are XML text files that store instances of my custom data class. These objects are serialized using the Simple XML Serialization library. In my android app, I'd like to instantiate objects from this XML serialization class.
I like to add these xml files to Android Studio so they are included in the APK on device install and are placed, for example, in the private app directory "files", to which getFilesDir() is mapped. I can't find a way to do that.
If I add these xml files to the Android XML resource folder, I need to use Android's XML resource parser, and can not use the Simple XML library.
Any tips? I feel I made a wrong design choice seeing how restrictive the resource bundling is.
Thanks, Kind regards,
Harmen
As per CommonsWare's comment: the solution was adding it to the raw resource folder, then you can access it using:
InputStream xmlExerciseInputStream = getResources().openRawResource(R.raw.myresource);
MyClass myClass = serializer.read(MyClass.class, xmlExerciseInputStream);
I have a big problem. Last I copied a CardView with RecyclerView from a video. After that I created a new empty activity, but the Javafile didn't want to accept the corresponding XML file. And if I go with ALT + ENTER he suggests to me to create your xml file, but afterwards he tells me it already exists. So I can open the XML file, the program recognizes it but it is not recognized in the Java data and I cannot select it there either. There a screen shot, There a picture from solution research, And There a photo of the xml File.
Can someone help me there? Thank you in advance!
I was facing the same problem but you just need to close your project and open it again, the error related to activity file will bee removed!!Just try it.....
maybe you need to rebuild your app
1- Build > clean project
2 - Build > Rebuild project
I hope it will work with you .
Delete those two files. Copy the code first.
Go to File ->new activity then create that activity and paste the code there, besides those lines.
There's a snippet of code in the Android sample's FacePreview class that boggles me:
File classifierFile = Loader.extractResource(getClass(),
"/org/bytedeco/javacv/facepreview/haarcascade_frontalface_alt.xml",
context.getCacheDir(), "classifier", ".xml");
I know I should change the names before haarcascade_frontalface_alt.xml to match my project name, but I can't figure out where to actually put the XML file in my project structure.
I did read somewhere that I have to put the xml file in the same folder as the FacePreview class... so does that mean I have to put it in src/main/java/<package_name>?
Edit: That doesn't work either. Do I have to download and cache it in the Activity?
it goes in src/main/resources/org/bytedeco/javacv/facepreview/haarcascade_frontalface_alt.xml
assuming gradle does the right thing
What kind of folder type is this and how can I create this?
The name is basically a source folder hierachy of a project library I include. But I do not know what this actually is and how to create this. Looking at the file structre in Finder I don't see anything. So I guess it must be some kind of link or so.
this is the symbol I try to understand ->
Many thanks for your help!
This is a folder Icon.
Top Right + mark kind indicates it is a source.
Bottom Right with ! indicates that there are some warnings in your code. Look for the Errorlog to remove the warnings.
This is created by Eclipse, as soon as you create a new package, which internally creates a folder for each part of the package name.
Example: If you have com.android.myproject
then eclipse, will create a folder for Src, inside this a com foler, then inside this an android folder and finally myproject folder.
i m working on a project in which i have to store and read some list elements in xml format i did the prasing part by creating file in res/xml and and using getResources().getXml(R.xml.SAMPLEFILE); it works well.. problem arise when i go for file creation..i m unable to find tutorial which lets u creat or open(if already created) xml in res/xml..
http://www.anddev.org/write_a_simple_xml_file_in_the_sd_card_using_xmlserializer-t8350.html
You cannot add anything to your res folder during runtime. If you want the xml to be stored there you'll have to add it in when you compile. If you need it to be dynamic then you'll have to store the xml file in a different place.
One option you have is private internal storage for your application. Check out this page
for an overview of how to save to and read from this internal storage.