1

我的应用程序使用数据并将其保存在文件 [root]/data/data/appName/files/list.xml

我知道如何解析 XML,如下所示:

XmlResourceParser parser = getResources().getXml(R.xml.list);

但是因为我有一个不在 res 目录中的文件,所以我需要找到另一种方法。

我知道如何将我的文件作为字符串获取,如下所示:

FileInputStream fIn = openFileInput("samplefile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[TESTSTRING.length()];
isr.read(inputBuffer);
String readString = new String(inputBuffer);

能够指定文件名很重要。

另外,当我保存文件时:

FileOutputStream fOut = openFileOutput("list1.xml", MODE_WORLD_READABLE);

编译器显示:"MODE_WORLD_READABLE"因为

"This constant was deprecated in API level 17". 

但它有效。这对我意味着什么?

4

1 回答 1

0

从路径读取 Xml 文件-

public boolean ReadXmlFile(String filePath)
    {
        try {
            String Data="";
            File fIN = new File(filePath);

            if (fIN.exists()) 
            {
                StringBuffer fileData = new StringBuffer(1000);
                BufferedReader reader = new BufferedReader(
                        new FileReader(filePath));
                char[] buf = new char[1024];
                int numRead=0;

                while((numRead=reader.read(buf)) != -1){            
                    String readData = String.valueOf(buf, 0, numRead);
                    fileData.append(readData);              
                    buf = new char[1024];
                }

                reader.close();   
                Data= fileData.toString();

            }
            else
            {

                return false;
            }

            docData = null;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

            try 
            {           
                DocumentBuilder db = dbf.newDocumentBuilder();          
                InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(Data));
                docData = db.parse(is);         
            } catch (ParserConfigurationException e) {          
                return false;
            } catch (SAXException e) {          
                return false;
            } catch (IOException e) {           
                return false;
            }
            return true;
        } catch (Exception e) {

            return false;
        }
    }
于 2013-04-23T08:47:46.893 回答