gpt4 book ai didi

java - JTidy java API toConvert HTML to XHTML

转载 作者:搜寻专家 更新时间:2023-10-31 23:07:45 25 4
gpt4 key购买 nike

我正在使用 JTidy 将 HTML 转换为 XHTML,但我在我的 XHTML 文件中发现了这个标记  。我可以阻止它吗?
这是我的代码

    //from html to xhtml
try
{
fis = new FileInputStream(htmlFileName);
}
catch (java.io.FileNotFoundException e)
{
System.out.println("File not found: " + htmlFileName);
}
Tidy tidy = new Tidy();
tidy.setShowWarnings(false);
tidy.setXmlTags(false);
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setXHTML(true);//
tidy.setMakeClean(true);
Document xmlDoc = tidy.parseDOM(fis, null);
try
{
tidy.pprint(xmlDoc,new FileOutputStream("c.xhtml"));
}
catch(Exception e)
{
}

最佳答案

只有当输入也被视为 XML 时,我才成功。所以要么将 xmltags 设置为 true

 tidy.setXmlTags(true);

并接受错误和警告或进行两次转换。第一次转换以清理 html(html 到 xhtml),第二次从 xhtml 转换为带有设置 xmltags 的 xhtml,因此不会出现错误和警告。

        String htmlFileName = "test.html";
try( InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(htmlFileName);
FileOutputStream fos = new FileOutputStream("tmp.xhtml");) {
Tidy tidy = new Tidy();
tidy.setShowWarnings(true);
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setXHTML(true);
tidy.setMakeClean(true);
Document xmlDoc = tidy.parseDOM(in, fos);
} catch (Exception e) {
e.printStackTrace();
}

try( InputStream in = new FileInputStream("tmp.xhtml");
FileOutputStream fos = new FileOutputStream("c.xhtml");) {
Tidy tidy = new Tidy();
tidy.setShowWarnings(true);
tidy.setXmlTags(true);
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setXHTML(true);
tidy.setMakeClean(true);
Document xmlDoc = tidy.parseDOM(in, null);
tidy.pprint(xmlDoc, fos);
} catch (Exception e) {
e.printStackTrace();
}

我用的是最新的jtidy 938版本

关于java - JTidy java API toConvert HTML to XHTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15063870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com