How to extract separate text nodes with Jsoup?

M.M :

I have an element like this :

<td> TextA <br/> TextB </td>

How can I extract TextA and TextB separately?

BalusC :

Several ways. That really depends on the document itself and whether the given HTML markup is consistent or not. In this particular example you could get the td's child nodes by Element#childNodes() and then test every node individually if it's a TextNode or not.

E.g.

Element td = getItSomehow();

for (Node child : td.childNodes()) {
    if (child instanceof TextNode) {
        System.out.println(((TextNode) child).text());
    }
}

which results in

 TextA 
 TextB 

I think it would be nice if Jsoup offered a Element#textNodes() or something to get the child text nodes like as Element#children() does to get the child elements (which would have returned the <br /> element in your example).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extract the text of same html tags in separate lines with JSoup on Android

how to extract text value using jsoup

How to extract text with Jsoup from table

How to extract text from wikipedia using Jsoup?

How to extract all text found in a webpage JSoup

How to extract text from inter DIV with Jsoup?

JSoup extract the text from the table td which doesnt contains any html nodes

How to extract text from specific rows in nested tables with Jsoup

How to extract tags and text between tags to a list with JSoup

How to extract text outside of a html tag using jsoup?

How to extract text from text nodes through Selenium?

How to extract text from child Text Nodes - Selenium

How to extract text sections into separate files named based on id?

How to extract a subset from a text file and store it in a separate file?

Extract text within HTML <br> tags JSOUP

how to separate children of nodes in a list

Replace text in all text nodes in a tree using Jsoup

In Jsoup how to change/split nodes during iteration

Xpath grabbing separate text in between link nodes

How to extract text from text nodes within same parent node through XPath

Remove nodes that don't contain their own text using Jsoup

JSOUP using Nodes to get specific text that is outside HTML tags

Xpath extract all the text between multiple nodes?

Extract and combine all text child nodes

Jsoup how to get specific Text

How to get orphaned text with Jsoup?

JSoup - How to extract only the href in paragraph

How to extract data in sequence using jsoup

How to extract elements from a String with jsoup?