假設你的文件是真的一個純文本文件不是XML
文件,你可以使用一個正則表達式提取XML
標籤之間的文本內容,爲未來:
Pattern pattern = Pattern.compile("<([^>]+)>([^<]*)</[^>]+>");
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
System.out.printf("%s: %s ", matcher.group(2).trim(), matcher.group(1));
}
System.out.println();
}
}
輸出:
<PersonName> Ian </PersonName> <OrgName> Cum Sociis Natoque Limited</OrgName>
Ian: PersonName Cum Sociis Natoque Limited: OrgName
<PersonName> Camilla </PersonName> <OrgName> Lorem Corporation </OrgName>
Camilla: PersonName Lorem Corporation: OrgName
<PersonName> Addison </PersonName> <OrgName> Tempus Corp. </OrgName>
Addison: PersonName Tempus Corp.: OrgName
<PersonName> Arsenio </PersonName> <OrgName> Id LLP </OrgName>
Arsenio: PersonName Id LLP: OrgName
在的Java 8 ,它應該是:
Pattern pattern = Pattern.compile("<([^>]+)>([^<]*)</[^>]+>");
try (Stream<String> stream = Files.lines(Paths.get(filePath))) {
stream.forEach(
line -> {
System.out.println(line);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
System.out.printf("%s: %s ", matcher.group(2).trim(), matcher.group(1));
}
System.out.println();
}
);
}
如果你使用java,那麼JAXB可能會幫助你。 –