你指的是DSL 寫在科特林通過建設者構建HTML。這個庫可以在這裏找到:https://github.com/Kotlin/kotlinx.html
這裏是一個正在運行的例子:
fun main(args: Array<String>) {
val document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
val html = document.create.html {
head {
title("Hello world")
}
body {
h1("h1Class"){
+"My header1"
}
p("pClass"){
+"paragraph1"
}
}
}
intoStream(html, System.out)
}
fun intoStream(doc: Element, out: OutputStream) {
with(TransformerFactory.newInstance().newTransformer()){
setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no")
setOutputProperty(OutputKeys.METHOD, "xml")
setOutputProperty(OutputKeys.INDENT, "yes")
setOutputProperty(OutputKeys.ENCODING, "UTF-8")
setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4")
transform(DOMSource(doc),
StreamResult(OutputStreamWriter(out, "UTF-8")))
}
}
最後,這裏的相應的輸出:
<?xml version="1.0" encoding="UTF-8"?><html>
<head>
<title>Hello world</title>
</head>
<body>
<h1 class="h1Class">My header1</h1>
<p class="pClass">paragraph1</p>
</body>
</html>
您的鏈接甚至不指向Koans。你正在尋找[這一個](https://try.kotlinlang.org/#/Kotlin%20Koans/Builders/Html%20builders/Task.kt)。但請注意左側的[html.kt](https://try.kotlinlang.org/#/Kotlin%20Koans/Builders/Html%20builders/html.kt)文件。 – chris
我認爲這個例子來自Koans,但您可能正在尋找[kotlinx.html'庫(https://github.com/Kotlin/kotlinx.html))。 – mkobit
@chris仍然沒有得到如何製作,沒有「主要」功能,如何調用它,以及如何顯示它。 –