这里有 4 个解决方案。
第一个可能是最好的:https://j2html.com/
// https://j2html.com/examples.html
@Test
public void test() {
String s = document(
html(
head(
title("Title"),
link().withRel("stylesheet").withHref("/css/main.css")),
body(
main(attrs("#main.content"),
h1("Heading!"))))).toString();
assertEquals("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"
+ "<html>\n"
+ " <head>\n"
+ " <title>\n"
+ " Title\n"
+ " </title>\n"
+ " <link type=\"text/css\" rel=\"stylesheet\" href=\"/css/main.css\">\n"
+ " </head>\n"
+ " <body>\n"
+ " <main id=\"main\" class=\"content\">\n"
+ " <h1>\n"
+ " Heading!\n"
+ " </h1>\n"
+ " </main>\n"
+ " </body>\n"
+ "</html>\n", JtidyTest.tidyHtml(s));
}
它不支持漂亮打印。为此使用 jtidy:
public static String tidyHtml(String inputHtml) {
Properties oProps = new Properties();
oProps.setProperty("new-blocklevel-tags", "main");
Tidy tidy = new Tidy();
tidy.setConfigurationFromProps(oProps);
tidy.setXHTML(false);
tidy.setDocType("loose");
tidy.setQuiet(true);
tidy.setShowWarnings(false);
tidy.setTidyMark(false);
tidy.setIndentContent(true);
StringWriter writer = new StringWriter();
tidy.parse(new StringReader(inputHtml), writer);
return writer.toString().replace("\r", "");
}
以下两个示例/解决方案仅使用 Java 自己的 XML 内容,因此不提供显式 HTML 支持,这有点不方便。
来自对象模型:
@Test
public void test() throws Exception {
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element htmlElement = doc.createElement("html");
htmlElement.setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
doc.appendChild(htmlElement);
Element headElement = doc.createElement("head");
htmlElement.appendChild(headElement);
Element titleElement = doc.createElement("title");
titleElement.appendChild(doc.createTextNode("The title of the page &"));
headElement.appendChild(titleElement);
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
+ "<head>\n"
+ "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
+ "<title>The title of the page &</title>\n"
+ "</head>\n"
+ "</html>\n",
transform(doc));
}
static String transform(Document doc) {
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString().replace("\r", "");
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
完整代码:https://github.com/jjYBdx4IL/java-evaluation/blob/master/src/test/java/tests/javax/xml/XhtmlGenTest.java
直接流式传输:
@Test
public void testGenerateXHTML() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter xml = outputFactory.createXMLStreamWriter(baos);
xml.writeStartDocument();
xml.writeStartElement("html");
xml.writeDefaultNamespace("http://www.w3.org/1999/xhtml");
xml.writeStartElement("head");
xml.writeStartElement("title");
xml.writeCharacters("The title of the page");
xml.writeEndElement();
xml.writeEndElement();
xml.writeEndElement();
xml.writeEndDocument();
assertEquals("<?xml version='1.0' encoding='UTF-8'?><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>The title of the page</title></head></html>",
baos.toString("UTF-8"));
}
完整代码:https://github.com/jjYBdx4IL/java-evaluation/blob/master/src/test/java/tests/javax/xml/stream/XMLStreamWriterTest.java
您也可以考虑 ECS,但它已停用,并且不支持对特殊字符(如 &)的隐式处理。我已将其更新为使用 Java 8 进行编译:https://github.com/jjYBdx4IL/misc/tree/master/ecs 不确定何时将其部署到 maven central tho。我还对其进行了修改以包含 javadoc、源代码和调试符号,所有这些都是 maven Central 上的现有发行版所不提供的。