Reputation: 315
Is there way to format html code present in java string to corresponding output which browser shows without browser in java itself? Ex: lets say string is,
<ul>
<li>red</span></li>
<li>green</span></li>
<li>orange<br /></span></li>
</ul>
<br />number list:<br />
<ol>
<li>one</li>
<li>two</li>
<li>three
<ul>
<li>embedded bullet
<ul>
<li>again
<ul>
<li>again
<ul>
<li>next one</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>four</li>
</ol>
This is O/P:
outputString="
• one
• two
• three
number list:
1. one
2. two
3. three
• embedded bullet
• again
• next one
4. four"
then formatter should convert this into outpuString string.
Upvotes: 1
Views: 83
Reputation: 17945
There is no built-in way to convert HTML to formatted-text in Java. Either you find a lynx-like (text-only) browser written in Java that you can use, or you will have to program one yourself.
If your input HTML is very simple and well-formed (as in your example), this is relatively straightforward. If you want to support wild HTML found online, this is a very complicated undertaking. Think CSS, Javascript, and column layouts that try to be responsive to how many horizontal pixels you are rendering in.
The easy part is parsing HTML, because there are many parsers that you can use. For example, I have used JSoup with success. The complicated part is rendering text that is similar to what you would see in an actual browser. Layout engines are one of the trickiest parts of browsers.
Upvotes: 2