-1

i have html code that looks like the following:

String html = "<html><head><style type=\"text/css\"></style></head><body><div style=\"font-family:times new roman,new york,times,serif;font-size:14pt\">first text<br><div><br></div><div style=\"font-family: times new roman,new york,times,serif; font-size: 14pt;\"><br><div style=\"font-family: times new roman,new york,times,serif; font-size: 12pt;\"><font size=\"2\" face=\"Tahoma\"><hr size=\"1\"><b><span style=\"font-weight: bold;\">one:</span></b> second text<br><b><span style=\"font-weight: bold;\">two:</span></b> third text<br><b><span style=\"font-weight: bold;\">three:</span></b> fourth text<br><b><span style=\"font-weight: bold;\">five:</span></b> fifth text<br></font><br>";

and i was wondering how to get the index of the third div ?

Teneff
  • 30,564
  • 13
  • 72
  • 103
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498

2 Answers2

4

I would use a DOM parser to do this. If you have proper XML (valid XHTML) content, you can use an XML parser as well.

And to which library to choose, this question helps you.

Update: as far as I can see, your content is not valid XML. So this question may be better for you.

Community
  • 1
  • 1
KARASZI István
  • 30,900
  • 8
  • 101
  • 128
0

A DOM/XML parser might be better, depending on what you need to do. But the simple way would be:

int index = -1;
for(int i=0; i < 3; i++){
   index = html.indexOf("<div",index + 1);
   if(index == -1)
      throw new Exception();
}
morja
  • 8,297
  • 2
  • 39
  • 59