Get all elements with Jsoup

1.1k views Asked by At

I'm trying to find all elements inside this kind of html:

<body>
My text without tag
<br>Some title</br>
<img class="image" src="url">
My second text without tag
<p>Some Text</p>
<p class="MsoNormal">Some text</p>
<ul>
<li>1</li>
<li>2</li>
</ul>
</body>

I need get all elements include parts without any tag. How a can get it?

P.S.: I need to get array of "Element" for each element.

2

There are 2 answers

0
nafas On

Not quite sure if you are asking to retrieve all the text within the html. to do that, you can simply do the following:

String html; // your html code
Document doc = Jsoup.parse(html); //parse the string
System.out.println(doc.text());   // get all the text from tags.

OUTPUT:

My text without tag Some title My second text without tag Some Text Some text 1 2

0
Kalyan Chavali On

Just in case if you using a html file, you can use the below code and retrieve each tag that you need. The API is Jsoup. You can find more examples in the below link http://jsoup.org/

File input = new File(htmlFilePath);

InputStream is = new FileInputStream(input);

String html = IOUtils.toString(is);

Document htmlDoc = Jsoup.parse(html);

Elements pElements = htmlDoc.select("P");

Element pElement1 = pElements.get(0);