我正在从互联网读取一个html文件,当我读取该文件时,控制台输出如下:
<string>
<String1>
text
</String1>
<level2>
text2
</level2>
<level3>
text3
</level3>
<level4>
text4
</level4>
<level5>
TEXT
</level5>
</string>
<string>
<String2>
text
</String2>
<level2>
text2
</level2>
<level3>
text3
</level3>
<level4>
text4
</level4>
<level5>
THIS TEXT
</level5>
</string>
我怎样才能在第二个字符串中访问level5文本?我已经尝试了一整天都没有成功,真的希望有人能给我提供一些有用的建议。
以下是我的代码:
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(String.valueOf(doc));
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
Elements tdElements = doc.getElementsByTag("level1");
for(Element element : tdElements )
{
//Print the value of the element
System.out.println(element.text());
}
}
// Always close files.
bufferedReader.close();
} catch (FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
doc + "'");
} catch (IOException ex) {
System.out.println(
"Error reading file '"
+ doc + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
//
catch (IOException e) {
e.printStackTrace();
}