将字符串的元素转换为html元素

html-css020

将字符串的元素转换为html元素,第1张

let str= <span class="keyword-lighten">aaa</span>

let ele = new DOMParser().parseFromString(str, "text/xml")

获取到document对象

然后ele.firstChild就是你想要的html元素来

String text = "字符内容"

String htmlFileName = UUIDUtil.generateUUID()+".html"

String htmlPath = imgPath+ File.separatorChar+htmlFileName

File file2 = new File(htmlPath)

StringBuilder sb = new StringBuilder()

try {

file2.createNewFile()//创建文件

sb.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/htmlcharset=utf-8\"><title>Html Test</title></head><body>")

sb.append("<div>")

sb.append(text)

sb.append("</div>")

sb.append("</body></html>")

PrintStream printStream = new PrintStream(new FileOutputStream(file2))

printStream.println(sb.toString())//将字符串写入文件

} catch (IOException e) {

code = 0

e.printStackTrace()

}