0

我有这样的输入:

<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>");

这不适用于所有情况,因为地址标签内部可能包含某些属性。请帮助如何获取此类字符串的文本。

4

3 回答 3

1

一般来说,您不应该使用正则表达式来解析 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 个计数。

要使这项工作与 a 一起使用,BufferedReader您可能必须确保一次读取一个完整<address>的标签。

于 2018-10-23T08:18:53.660 回答
0

您可以将文件转换为字符串,并可以确定所需子字符串的开始和结束索引,如下所示:

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);
        }
    }
}
于 2018-10-23T08:07:32.243 回答
0
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();

...... 算法一定是这样的。如果您阅读文件。

于 2018-10-23T08:45:01.770 回答