I have seen lots of examples about parsing local xml files with Sax but i need to do this with DOM. My xml file is under res/xml folder. Here is the code:
InputStream in = getResources().openRawResource(R.xml.maps);
StringBuffer str = new StringBuffer();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
DocumentBuilder db = dbf.newDocumentBuilder ();
Document document = db.parse(in, null);
document.getDocumentElement ().normalize ();
But it doesnt work. I think something missing with the line InputStream. It is really important for me, i need your help.
I had the same. I was wondering why this openRawResource gets the XML file corrupted, and that's because it was placed in r.xml.* not in r.raw.* :)
I have solved the problem. Just need to change small thing:
InputStream in = this.getResources().openRawResource(R.raw.bu);
I added "this" to that line and also I have put the maps.xml file under res/raw.
Related
I am working on an android application in which I want to parse an xml file as a local resource. I use jdom to parse it, but I have a probleme, I can not open the file and I don't know why. The error is at this line :
document = builder.build(new File("res/xml/data.xml"));
The file is located into the folder res/xml of my project. I got this error:
java.io.IOException: Couldn't open file:/res/xml/data.xml
Caused by java.io.FileNotFoundException :res/xml/data.xml
I tried this:
document = builder.build(new File("data.xml"));
but it did not work. I don't know why the file is not found.
Would you have an idea?
Thanks in advance
Thank you for your suggestion. The 'Uri path' works but not the line
document = builder.build(new File(path));
The method class File must have a type entree String. I tried this:
document = builder.build(new File(path.getPath());
but it returned null and
document = builder.build(new File(path.toString());
I got the same error. Would you have an idea ?
Thanks in advance !!
I believe you will need to use an URI in your file constructor that points to the data.xml file.
Try something like this:
Uri path = Uri.parse("android.resource://my.package.here/" + R.xml.data);
document = builder.build(new File(path));
This tutorial here might also be helpful: http://androidbook.blogspot.com/2009/08/referring-to-android-resources-using.html
If this is a resource inside your application then you should use Resources methods suchas getXml(). You can get an instance of the rources class from the Context object
InputStream is = context.getResources().openRawResource(R.raw.data);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
document = builder.build(br);
Be sure to put the data.xml in the res/raw-folder.
I put my employeedetailxml into a folder named "res\raw". When I attempt to open it by specifying the file and folder name, I get the error "file not found".
How can I specify the path to my file?
I would much prefer to be able to pass 'R.raw.employeedetailxml' to FileInputStream to specify that it open that file. Is that possible? (I tried it, and got an error.)
Can FileInputStream take a Resource ID as parameter?
try{
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp=spf.newSAXParser();
XMLReader xr=sp.getXMLReader();
EmployeeDetailHandler empDetailHandler=new EmployeeDetailHandler();
xr.setContentHandler(empDetailHandler);
xr.parse(new InputSource(new FileInputStream("\\res\\raw\\employeedetailxml.xml")));
}
Yes, you can use something similar to passing R.raw.employeedetailxml. You just need to fetch the resource XML file using that resource name, like so:
InputStream ins = getResources().openRawResource(R.raw.file_name);
Try this:
InputStream inputStream =
getResources().openRawResource(R.raw.employeedetailxml);
this.getResources().openRawResource() returns an InputStream that will probably help you to parse the xml.
If placing your xml file under assets doesn't hurt, you can try out this one:
AssetManager manager = getAssets();
InputStream inputStream = manager.open("pathUnderAssets/filename.xml");
Please note that getAssets() call is made on context. So, you must call it with context in classes which is not an Activity.
I can open the xml files from my local folder of an android project in Eclipse to read the values:
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(R.xml.tittes);
But how could I open a local xml file to edit the file and save it after?
Thanks for the replies!.,
I mean with a local file, a XML file I need to read and edit for data storage purposes. I've decided to place the file in the asset folder and I can open it easily for read::
*DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
InputStream iS = this.getAssets().open("myFile.xml");
Document doc = docBuilder.parse(iS);*
...,, but I don`t obtain, after many attemps, the way to save the file to edit: My last attempt is:
*TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();
OutputStream f0;
byte buf[] = xmlString.getBytes();
**f0 = new FileOutputStream("/assets/wines.xml");**
//f0 = new FileOutputStream("file:///assets/wines.xml");
for(int i=0;i<buf .length;i++) {
f0.write(buf[i]);
}
f0.close();
buf = null;*
and the failed result is File not found!!!,. I come from C# projects,.and I couldn`t imagine that save a modified file was so dificult,.,
any ideas?!¿!? thanks again!!
but anyone know how could I open a local xml file to edit the file and save it after.
If by "local xml file" you mean a file in getFilesDir(), use DOM, or SAX, or the XmlPullParser.
If by "local xml file" you mean a resource, and you want to modify that resource, that is not possible at runtime.
There is no way to edit xml resources in the res folder in your program.
You can try another way, such as saving your resources to SharedPreference or SQLite at the first time your app starts, and later on you just edit and use your own copy.
This is useful when you use the resource to fill some component, but not good for layouts.
I parsed xml data successfully several times before, however its my first time doing the same in android. I tried using DOM as I usually do however an error occurs on the .parse method each time. I searched online and tried multiple times however without any success. All I need to do is get xml data from a local file to put as arguments in some functions. I implemented the code in the Main Activity's onCreate method. This is what I usully do, however this time without any success:
try {
File xmlFile = new File("C:\\Users\\gg90\\Desktop\\testingxml.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
Hope you can help me out. Thanks!
There is no notion of a drive C: in android, so I guess your file is invalid and the parser chokes on it. Android uses a unix file system, so path look like /data/file.xml and so on.
To parse an xml file in the assets folder, have a look at this code, which opens the folder, gets the file and opens an input stream. And then calls this method.
1./ in res, create new folder: raw == /res/raw
copy file testingxml.xml and paste to folder raw
2./ parse XML from the location above by using SOM (ou SAX if you want)
InputStream catDoc = context.getResources().openRawResource(R.raw.testingxml);
//Using factory get an instance of document builder
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//parse using builder to get DOM representation of the XML file
dom = builder.parse(catDoc, null);
Try creating a folder named raw in the res folder and put the xml file there through your project in workspace. Refresh the project until it is updated in R.java. Then use this code:
InputStream file = this.getResources().openRawResource(R.raw.license_status);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
so go to your project folder and find RES folder then make a new folder there named "RAW"
and place your XML file there and refresh the project until it is updated in automatically generated R.java as a class.
Now you can use R.RAW.XYZ.xml ... :)
I've found through research on google that that I can read a text file by storing it in my res/raw folder and then accessing it through getResources().openRawResource(R.raw.words);
In a class WordHelper I the constructor provides throws an InvocationException on this line of code:
istream = getResources().openRawResource(R.raw.words);
Which is the one most examples seem to use without a problem, I will then go on to do this
isreader = new InputStreamReader(istream);
myReader = new BufferedReader(isreader);
once everything is working and then use the readLine() method.
All descriptions of InvocationException such as getCause are null, I definately have the file in the res/raw/words.txt.
Thanks for reading.
I got the same problem and solved it using isreader = new InputStreamReader(istream, "UTF8");