2012-02-13 137 views
4

我有一个名为bkup.xml的xml文件存储在sdcard“/sdcard/bkup.xml”中。从SD卡读取.xml文件

创建bkup.xml我已经使用了xmlSerialization。

我想从该bkup.xml文件检索数据。

我见过很多例子,但其中大部分例子都是使用资源文件并使用URL作为资源。但没有人给出sdcard文件路径的例子。

我不知道如何从该文件中获取数据并解析它。

在此先感谢。

任何建议将理解

+0

发表你的代码你还有什么尝试过吗 – ingsaurabh 2012-02-13 12:25:43

+0

你能告诉我任何接受文件位置解析路径的方法吗? – 2012-02-13 12:27:48

回答

5

Here是与源的完整的示例。您只需要使用

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

然后进行进一步处理。

UPDATE

如果您需要例如,对于different类型XMLParsers可以downloadcompletehere例子。

+1

上面的代码帮助我解决了这个问题。 谢谢.. – 2012-02-14 10:38:57

0

使用的FileReader对象是这样的:

/** 
    * 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(); 
    } 

还需要添加权限访问设备上的SD卡。