如何读取 XML 文件并将其作为带有所有标签和白色字符的字符串传递给 setText [Android]
How to read XML file and pass to setText as String with all tags and white-characters [Android]
我想读取 XML 文件,将其保存为字符串并传递给 setText。我不想解析它,但在我的智能手机屏幕上看到它带有所有标签和白色字符,例如
<a>
<b>some text</b>
</a>
不是:
some text
怎么做?
使用FileInputStream
读取文件:
File file = new File(<FilePath>);
if (!file.exists()) {
System.out.println("File does not exist.");
return;
}
if (!(file.isFile() && file.canRead())) {
System.out.println(file.getName() + " cannot be read from.");
return;
}
try {
FileInputStream stream = new FileInputStream(file);
char current;
while (stream.available() > 0) {
current = (char) stream.read();
//Do something with character
}
} catch (IOException e) {
e.printStackTrace();
}
仅供参考,这是我解决问题的方法:
public String readXML() {
String line;
StringBuilder total = new StringBuilder();
try {
InputStream is = activity.getAssets().open("subjects.xml");
BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
total = new StringBuilder();
while ((line = r.readLine()) != null) {
total.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return total.toString();
}
我想读取 XML 文件,将其保存为字符串并传递给 setText。我不想解析它,但在我的智能手机屏幕上看到它带有所有标签和白色字符,例如
<a>
<b>some text</b>
</a>
不是:
some text
怎么做?
使用FileInputStream
读取文件:
File file = new File(<FilePath>);
if (!file.exists()) {
System.out.println("File does not exist.");
return;
}
if (!(file.isFile() && file.canRead())) {
System.out.println(file.getName() + " cannot be read from.");
return;
}
try {
FileInputStream stream = new FileInputStream(file);
char current;
while (stream.available() > 0) {
current = (char) stream.read();
//Do something with character
}
} catch (IOException e) {
e.printStackTrace();
}
仅供参考,这是我解决问题的方法:
public String readXML() {
String line;
StringBuilder total = new StringBuilder();
try {
InputStream is = activity.getAssets().open("subjects.xml");
BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
total = new StringBuilder();
while ((line = r.readLine()) != null) {
total.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return total.toString();
}