<h2>This is a heading</h2>
<p>My mother has
<span style="color:blue;font-weight:bold">blue</span>
eyes and my father has
<span style="color:darkolivegreen;font-weight:bold">
dark green</span>
eyes.</p>
<h3>This is another heading<h3>
<p>This is a paragraph</p>
"My mother has blue eyes and my father has dark green eyes". I want to parse this sentance using JSOUP and print the same with bold and coloured text on an android textview. Here "blue" is bold and in blue color. "Dark Green" is bold and in color.
I need to parse the above html code and need to display like this :
This is a heading
My mother has blue eyes and my father has dark green eyes
This is another heading
This is a paragraph
Below is my program. Considering Document doc == get above html;
Elements eHeadder = doc.select("*");
for (Element eHead : eHeadder) {
String tag = eHead.tagName();
if (tag.equals("p")) {
String pText = eHead.text();
tv.setText(pText);
}else if(tag.equals("h2")){
String pText = eHead.text();
tv.setText(pText);
}else if(tag.equals("h3")){
String pText = eHead.text();
tv.setText(pText);
}
}
Is this what you ment?
public static void main(final String[] args)
{
final String html = "<p>My mother has\n" +
"<span style=\"color:blue;font-weight:bold\">blue</span>\n" +
"eyes and my father has\n" +
"<span style=\"color:darkolivegreen;font-weight:bold\">\n" +
"dark green</span>\n" +
"eyes.</p>\n" +
"<h2>Mr. <span style=\"color:green\">Foobar</span></h2>";
final Document document = Jsoup.parse(html);
final Elements textNodes = document.select("p,h2");
for (final Element element : textNodes)
{
System.out.println("Found: " + element.text());
System.out.println("\t Neasted Spawns:");
for (final Element span : element.select("span"))
{
System.out.println("\t\t css: " + span.attr("style"));
}
}
}
It will print:
Found: My mother has blue eyes and my father has dark green eyes.
Neasted Spawns:
css: color:blue;font-weight:bold
css: color:darkolivegreen;font-weight:bold
Found: Mr. Foobar
Neasted Spawns:
css: color:green
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.