Limpep
Limpep

Reputation: 499

parsing string to get content

I have the following html string:

<h3>I only want this content</h3> I don't want this content <b>random content</b>

And I would like to only get the content from the h3 tags and remove the other content. I have the following:

String getArticleBody = listArt.getChildText("body");
StringBuilder mainArticle = new StringBuilder();
String getSubHeadlineFromArticle;

if(getArticleBody.startsWith("<h3>") && getArticleBody.endsWith("</h3>")){
    mainArticle.append(getSubHeadlineFromArticle);
 }

But this returns the whole content, which is not what I am after. If someone could help me that would be great thanks.

Upvotes: 0

Views: 259

Answers (6)

Limpep
Limpep

Reputation: 499

Thanks, guys. All your answers worked, but I ended up using Jsoup.

String getArticleBody = listArt.getChildText("body");
org.jsoup.nodes.Document docc = Jsoup.parse(getArticleBody);
org.jsoup.nodes.Element h3Tag = docc.getElementsByTag("h3").first();
String getSubHeadlineFromArticle = h3Tag.text();

Upvotes: 1

octothorpentine
octothorpentine

Reputation: 319

The other answers already cover how to get the result you want. I'm gonna comment your code to explain why it isn't doing that already. (Note that I modified your variable names because strings don't get anything; they are a thing.)

// declare a bunch of variables
String articleBody = listArt.getChildText("body");
StringBuilder mainArticle = new StringBuilder();
String subHeadlineFromArticle;

// check to see if the article body consists entirely of a subheadline
if(articleBody.startsWith("<h3>") && articleBody.endsWith("</h3>")){
    // if it does, append an empty string to the StringBuilder
    mainArticle.append(subHeadlineFromArticle);
}
// if it doesn't, don't do anything

// final result:
//   articleBody = the entire article body
//   mainArticle = empty StringBuilder (regardless of whether you appended anything)
//   subHeadlineFromArticle = empty string

Upvotes: 0

ferrerverck
ferrerverck

Reputation: 628

You need to use regex like this:

public static void main(String[] args) {
    String str = "<h3>asdfsdafsdaf</h3>dsdafsdfsafsadfa<h3>second</h3>";
    // your pattern goes here
    // ? is important since you need to catch the nearest closing tag
    Pattern pattern = Pattern.compile("<h3>(.+?)</h3>"); 
    Matcher matcher = pattern.matcher(str);
    while (matcher.find()) System.out.println(matcher.group(1));
}

matcher.group(1) returns exactly text between h3 tags.

Upvotes: 0

Keval Trivedi
Keval Trivedi

Reputation: 1300

Using regular expression
It may helps you :

String str = "<h3>I only want this content</h3> I don't want this content <b>random content</b>";
final Pattern pattern = Pattern.compile("<h3>(.+?)</h3>");
final Matcher matcher = pattern.matcher(str);
matcher.find();
System.out.println(matcher.group(1)); // Prints String I want to extract

Output :

I only want this content

Upvotes: 0

Wundwin Born
Wundwin Born

Reputation: 3475

Try with this

String result = getArticleBody.substring(getArticleBody.indexOf("<h3>"), getArticleBody.indexOf("</h3>"))
                .replaceFirst("<h3>", "");
System.out.println(result);

Upvotes: 0

Ninad Pingale
Ninad Pingale

Reputation: 7079

You can use substring method like this -

String a="<h3>I only want this content</h3> I don't want this content <b>random content</b>";
System.out.println(a.substring(a.indexOf("<h3>")+4,a.indexOf("</h3>")));

Output -

I only want this content

Upvotes: 0

Related Questions