how to get text from html where between <ol><font></font></ol> by Java
时间: 2023-08-17 11:49:24 浏览: 98
To extract text from HTML that is enclosed within `<ol><font></font></ol>` tags using Java, you can utilize a library like Jsoup. Here's an example code snippet:
```java
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HtmlTextExtractor {
public static void main(String[] args) {
String html = "<html><body><ol><font>This is the text to extract</font></ol></body></html>";
// Parse the HTML string
Document doc = Jsoup.parse(html);
// Find all <ol> elements
Elements olElements = doc.select("ol");
for (Element olElement : olElements) {
// Find <font> elements within each <ol> element
Elements fontElements = olElement.select("font");
for (Element fontElement : fontElements) {
// Extract the text from the <font> element
String extractedText = fontElement.text();
System.out.println("Extracted Text: " + extractedText);
}
}
}
}
```
In this example, we use the Jsoup library to parse the HTML string. We then use CSS selectors to find all `<ol>` elements and within each `<ol>` element, we find `<font>` elements. Finally, we extract the text from the `<font>` element using the `text()` method.
Make sure you have added the Jsoup library to your Java project for this code to work. You can download Jsoup from the official website or add it as a dependency using a build tool like Maven or Gradle.
阅读全文