Programmatic HTMLDocument generation using Java

前端 未结 9 1887
孤独总比滥情好
孤独总比滥情好 2021-02-14 03:34

Does anyone know how to generate an HTMLDocument object programmatically in Java without resorting to generating a String externally and then using HTMLEditorKit#read to parse i

9条回答
  •  暖寄归人
    2021-02-14 04:24

    One object-oriented approach is to use a library called ECS.

    It is quite simple library, and has not changed for ages. Then again, the HTML 4.01 spec has not changed either ;) I've used ECS and consider it far better than generating large HTML fragments with just Strings or StringBuffers/StringBuilders.

    Small example:

    Option optionElement = new Option();
    optionElement.setTagText("bar");
    optionElement.setValue("foo");
    optionElement.setSelected(false);   
    

    optionElement.toString() would now yield:

    
    

    The library supports both HTML 4.0 and XHTML. The only thing that initially bothered me a lot was that names of classes related to the XHTML version started with a lowercase letter: option, input, a, tr, and so on, which goes against the most basic Java conventions. But that's something you can get used to if you want to use XHTML; at least I did, surprisingly fast.

提交回复
热议问题