Convert any object to pretty HTML in java

How can I convert a given object (in general view with reflection) into pretty printed HTML? In which finished library do you recommend this? I need support for simple nested objects (until they create loops in the object graph).

I tried converting it to JSON, but DefaultPrettyPrinter does not support HTML.

+7
java html
source share
3 answers

You can write your own library for this relatively easily. Here are some sample code that you could relatively easily modify to show html. Another option is to display JSON inside the code tag in html. One of the last options is to use ReflectionToStringBuilder in apache commons lang and show the result again inside the code tag in html. Using apache commons is probably no better than json format.

+3
source share

You can create an XSLT style to output xstream XML to your object.

+7
source share

It can be performed in a two-step process:

1) Convert the object to JSON using any of your library, for example. jackson :

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String json = ow.writeValueAsString(object); 

2) Convert the JSON string to HTML using a ready-made solution like json2html or do your own implementation. Take a look at the visualizer demo:

0
source share

All Articles