There is a code for parsing and making some changes to the displayed text:

Document doc; try { doc = Jsoup.connect(contentUrl).get(); imagesRec = doc.select("div.post-featured-img amp-img[src~=(?i)\\.(png|jpe?g|gif)]"); imgSrcUrl = imagesRec.attr("abs:src"); contentPrepared = new StringBuilder(); for (Element p : doc.select("div.amp-wp-article-content p, div.amp-wp-article-content h2, div.amp-wp-article-content h3, div.amp-wp-article-content cite")) { contentPrepared.append(p.text()); contentPrepared.append("\n" + "\n"); } for (Element p : doc.select("div.amp-wp-article-content li")) { contentPrepared.append(p.text()); contentPrepared.append("-----"); } contentFinal = contentPrepared.toString(); 

As the code shows, I want to take the classes div.amp-wp-article-content p , div.amp-wp-article-content h2 , div.amp-wp-article-content h3 and div.amp-wp-article-content cite and insert new lines between them, and it works great.

The whole problem is in the second "insert", more precisely in the final form of the text. I want to take the class div.amp-wp-article-content li and insert "-----" before it. All this works, but when displaying, the contents of the tags from the first StringBuilder first StringBuilder , and then, at the end of the page, the contents of the tags from the second StringBuilder .

Question: how to display elements in the order in which they should be on the page? Those. The text inside the div.amp-wp-article-content li tag should not be located at the bottom of the page, but between the tags from the first StrinBuilder.

  • For this, you need to add elements to the StringBuilder in the order in which you want them to be displayed on the page. - Denis
  • So the elements from the second builder are located between the elements of the first builder, that’s the problem. - Artem Ilyinsky
  • Combine the search into one select and inside the loop, make a check of the element: if it is li - add "-----" , if not - "\n\n" - woesss
  • @woesss Could you give an example of this condition, if not difficult? - Artyom Ilyinsky
  • for (Element p: doc.select ("div.amp-wp-article-content li, div.amp-wp-article-content p, div.amp-wp-article-content h2, div.amp-wp-article -content h3, div.amp-wp-article-content cite ")) {contentPrepared.append (p.text ()); if (p.text (). contains ("div.amp-wp-article-content li")) {contentPrepared.append ("-------"); } else {contentPrepared.append ("\ n" + "\ n"); }} naturally, it doesn't work as it should. - Artem Ilyinsky

0