0

I've been looking here in stackoverflow how can I search for a String part inside big texts. But I haven't managed to find how to get an specific value of an attribute inside a Script using Java. The goal is read a file (script) line by line, and extract the value of an attribute "src".

For instance, the file has many lines containing this structure:

<script src="js/vendor/modernizr-2.6.2.min.js"></script>
<script data-main="js/" src="js/require.min.js"></script>
<script data-main="js/" src="js/main.js"></script>
<script src="js/vendor/modernizr-2.6.2.min.js"></script>
<script data-main="js/" src="js/require.min.js"></script>

So, using Java I read the file this way using BufferedReader class, I want to get for each line the value of "src", for example, for the first line, I want to get: js/vendor/modernizr-2.6.2.min.js, for the second line, I want to get js/require.min.js and so on, I saw some suggestions like using regex, but I don't know if it is the most effective in this cases:

public Helper(String scriptPath) {
        File scriptFile = null;
        try {
            scriptFile = new File(scriptPath);
            String relativePath = scriptFile.getParent();
            System.out.println(relativePath);
            BufferedReader reader = new BufferedReader(new FileReader(scriptFile));
            String readLine;

            while ((readLine = reader.readLine()) != null) {
                // How to match the src?
            }

            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Please, if somebody could help me I will really appretiate it or if someone knows that there's already an answer for this, please let me know in order to close this, but at the time I've been searching, I haven't found this kind of problem yet.

Thank you very much in advance.

4

1 回答 1

0

Your file looks like html I would consider using an Html Parser. http://jsoup.org/ is very easy to use with css like selectors

于 2013-04-29T12:57:30.107 回答