Tuesday 16 August 2016

Java - Extract data between html tags

/**
* This function helps in getting contents spans html tag;
* For ex., <body><b>Sample Text</b></body> will result in <b>Sample Text</b>
* @param content html content;
* @param startTag starting tag with html content, Ex., <body
* @param endTag ending tag Ex., </body>
* @return String
*/
public static String getPartialHtmlContent(String content, String startTag, String endTag) {
int startIndex = content.indexOf(startTag);
//Tag might contain attributes
startIndex = content.indexOf(">", startIndex);
int endIndex = content.indexOf(endTag);

if(startIndex > 1 && endIndex > 1)
return content.substring(startIndex + 1, endIndex);
return content;
}


No comments:

Post a Comment

About Me

Popular Posts

Designed ByBlogger Templates