Reputation: 1
If you have a HTML page stored in String ArrayList, and you want to for example read the whole <div>
tag of certain class type, how do you read the next lines so that it would reach the end of div tag?
for (String l : line) {
if (l.contains("<div class=\"somne_class\">"){
//read the next n strings in ArrayList until </div> tag is reached
}
Upvotes: 0
Views: 75
Reputation: 2891
Generally, it's bad idea to store HTML file as list of raw strings. Why do you store it in such way?
Imagine you have string like <div id="outer_div"><div id="inner_div">Hei!</div></div>
. Here you have multiple nested HTML tags in a single line, so you won't easily get the closing tag.
Consider using HTML parser, then you can get desired tag(s) by type or attribute. There are plenty of HTML parsers implemented in Java. One of the most popular is jsoup.
Upvotes: 1
Reputation: 7255
I recommend you to use jsoup
It is nice for parsing an writing html file.Althought i hadn't yet digged to much on it here is an example of taking all the elements with tag div:
Document htmlFile = null;
// Read the html file
try {
htmlFile = Jsoup.parse(new File("path"),"UTF-8");//path,encoding
} catch (IOException e) {
e.printStackTrace();
}
Elements images = htmlFile.getElementsByTag("div");
You can do much more read here
Upvotes: 0
Reputation: 5044
I agree with Vladimir, you're probably looking for an HTML parser.
To answer the exact question in the post: to simply find the next </div>
tag, you can use a for loop instead of a foreach loop.
for (int i = 0; i < line.size(); ++i) {
String l = line.get(i);
if (l.contains("<div class=\"somne_class\">") {
for (int j = i; j < line.size(); ++j) {
String l2 = line.get(j);
if (l2.contains("</div>")) {
// l2 is the next line that contains a </div> tag
}
}
}
}
Note that this might not be the matching closing tag for the opening tag, even if you assume that every tag is in a different line.
Upvotes: 1