[英]Get text between two tags using substringBetween() method of StringUtils
我有這樣的輸入:
<address>
<addressLine>280 Flinders Mall</addressLine>
<geoCodeGranularity>PROPERTY</geoCodeGranularity>
</address>
<address type="office">
<addressLine>IT Park</addressLine>
<geoCodeGranularity>office Space</geoCodeGranularity>
</address>
我想捕獲地址標簽之間的所有內容。
我試過了:
File file = new File("test.html");
String testHtml = FileUtils.readFileToString(file);
String title = StringUtils.substringBetween(testHtml, "<address>", "</address>");
這不適用於所有情況,因為地址標記內部可能包含一些屬性。 請幫助如何獲取此類字符串的文本。
一般來說,你不應該使用正則表達式來解析HTML / XML的內容。 而是使用XPath之類的解析器。 鑒於您似乎無法使用解析器,我們可以使用模式匹配器嘗試以下選項:
int count = 0;
String input = "<address>\n<addressLine>280 Flinders Mall</addressLine>\n <geoCodeGranularity>PROPERTY</geoCodeGranularity>\n</address>\n<address type=\"office\">\n <addressLine>IT Park</addressLine>\n <geoCodeGranularity>office Space</geoCodeGranularity>\n</address>";
String pattern = "<address[^>]*>(.*?)</address>";
Pattern r = Pattern.compile(pattern, Pattern.DOTALL);
Matcher m = r.matcher(input);
while (m.find( )) {
count += m.group(1).length();
System.out.println("Found value: " + m.group(1) );
}
System.out.println("count = " + count);
這將為您的示例數據中的兩個<address>
標記找到198個計數。
為了使此功能與BufferedReader
一起使用,您可能必須確保一次讀取一個完整的<address>
標記。
您可以將文件轉換為String,並可以如下確定所需子字符串的開始和結束索引:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Address {
public static void main(String[] args) throws IOException {
// Complete File Path
File dir =
new File("\\..\\..\\Test.html");
// Convert File Data As String
String data =
new String(
Files.readAllBytes(Paths
.get(dir
.getAbsolutePath())));
// For Loop to get all the <address> tags in the file.
for (int index = data.indexOf("<address"); index >= 0;) {
// Start Index
int startIndex = data.indexOf(">", index + 1);
++startIndex;
// End Index
int indexOfEnd = data.indexOf("</address>", startIndex + 1);
String attributesString = data.substring(startIndex, indexOfEnd);
// Replace below line with desired logic with calling trim() on the String attributesString
System.out.println(attributesString);
// Next Address will be after the end of first address
index = data.indexOf("<address", indexOfEnd + 1);
}
}
}
while (scan.hasNextLine()) {
parser = scan.nextLine();
// System.out.println(parser);
if (parser.equals("<adress>")) {
parser = scan.nextLine();
// System.out.println(parser);
int startPosition = parser.indexOf("<adressLine>") + "<adressLine>".length();
int endPosition = parser.indexOf("</adressLine>", startPosition);
idNumber = parser.substring(startPosition, endPosition);
parser = scan.nextLine();
int startPosition1 = parser.indexOf("<geoCodeGranularity>") + "<geoCodeGranularity>".length();
int endPosition1 = parser.indexOf("</geoCodeGranularity>", startPosition1);
time = parser.substring(startPosition1, endPosition1);
parser = scan.nextLine();
......算法必須是這樣的。 如果您在文件上閱讀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.