Reading XML file with SDK

I have one xml file called bkup.xml stored inside sdcard "/sdcard/bkup.xml".

To create bkup.xml, I used xmlSerialization.

I want to get data from this bkup.xml file.

I have seen many examples, but almost most of them use a resource file and use a URL as a resource. But no one has an example to give the path to the sdcard file.

I do not know how to extract data from this file and analyze it.

Thanks in advance.

Any suggestion will be appreciated.

+4
source share
2 answers

Here is a complete source example. You just get File using

  File file = new File(Environment.getExternalStorageDirectory() + "your_path/your_xml.xml"); 

Then do further processing.

UPDATE

If you need an example for different types of XML Parsers , you can download complete example from Here .

+5
source

Use the FileReader object as follows:

 /** * Fetch the entire contents of a text file, and return it in a String. * This style of implementation does not throw Exceptions to the caller. * * @param aFile is a file which already exists and can be read. * File file = new File(Environment.getExternalStorageDirectory() + "file path"); */ static public String getContents(File aFile) { //...checks on aFile are elided StringBuilder contents = new StringBuilder(); try { //use buffering, reading one line at a time //FileReader always assumes default encoding is OK! BufferedReader input = new BufferedReader(new FileReader(aFile)); try { String line = null; //not declared within while loop /* * readLine is a bit quirky : * it returns the content of a line MINUS the newline. * it returns null only for the END of the stream. * it returns an empty String if two newlines appear in a row. */ while (( line = input.readLine()) != null){ contents.append(line); contents.append(System.getProperty("line.separator")); } } finally { input.close(); } } catch (IOException ex){ ex.printStackTrace(); } return contents.toString(); } 

You also need to add permission to access the SDCard on the device.

0
source

Source: https://habr.com/ru/post/1396186/


All Articles