1

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&nbsp;
                            <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.

4
  • 1
    What exactly does that mean? A browser shows a page with layout elements, not a simple String. Can you edit the question to include the exact string you expect to be produced from your example? Commented Oct 8, 2018 at 8:24
  • You have included an image of the formatted document. If you really want to generate a similar image automatically, you can feed the html text into java's built-in JTextEditor, and render its output into an image. Would this be acceptable? Commented Oct 8, 2018 at 9:22
  • @tucuxi i have included output string below code snippet. what i basically want is to convert string with html code into the output which browser shows but on java side only.Above outputString is created by using unicode characters like(• , ◦). Commented Oct 8, 2018 at 9:51
  • This isn't something I 'd use XSL-FO for, so I've removed that tag. Commented Oct 10, 2018 at 14:51

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The real question becomes - in what, or for what format/viewer do you intend use this output? If you are planning to read this output in Word, for instance, the answer is totally different then if you want to view this output in a Text-File vs. Some-Other-Browser, vs. input to another programming language... ! Ultimately, if this text is going to be read, then eventually sending this back to a browser begs the question, why do you want to remove the formatting in the first place!? JMHO :)
I have found solution to this issue, i wanted to generate pdf from xml file (using fop & xsl ) which had html inside some elements , so this html used directly get printed in pdf. I found one xhtml2fo stylesheet which has all the templates required to convert html inside xml elements to proper output in pdf.
However, pdf is not text-with-spaces-and-newlines; and xml is not (in general) html. Text in pdf includes a lot of font and positioning information. And yes, there are multiple libraries for rendering xml via stylesheet into pdfs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.