[英]How to parse xml from NOT resource file
My app works with data and saves it in the file [root]/data/data/appName/files/list.xml 我的应用程序处理数据并将其保存在文件[root] /data/data/appName/files/list.xml中
I know how to parse the XML, like this: 我知道如何解析XML,如下所示:
XmlResourceParser parser = getResources().getXml(R.xml.list);
but because I havea file not in res dir, I need to find another way. 但是因为我有一个不在res目录中的文件,所以我需要找到另一种方法。
I know how to get my file as a string, like this: 我知道如何以字符串形式获取文件,如下所示:
FileInputStream fIn = openFileInput("samplefile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[TESTSTRING.length()];
isr.read(inputBuffer);
String readString = new String(inputBuffer);
It is important to be able to specify the name of file. 能够指定文件名很重要。
Also, when I save file with: 另外,当我使用以下命令保存文件时:
FileOutputStream fOut = openFileOutput("list1.xml", MODE_WORLD_READABLE);
The compiler shows: "MODE_WORLD_READABLE"
because 编译器显示:
"MODE_WORLD_READABLE"
因为
"This constant was deprecated in API level 17".
But it works. 但这有效。 What does it mean for me?
对我来说意味着什么?
Read Xml File From Path- 从路径读取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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.