我的应用程序处理数据并将其保存在文件[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".
但它有效。这对我意味着什么?
答案 0 :(得分: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;
}
}