【问题标题】:JTidy java API toConvert HTML to XHTMLJTidy java API toConvert HTML to XHTML
【发布时间】:2013-02-10 09:55:45
【问题描述】:

我正在使用 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)  
    {  
    }

【问题讨论】:

  • 你使用的是哪个版本的 jtidy,你是怎么称呼它的?
  • @Waxolunist 谢谢我,编辑了问题,这样你就可以看到我的代码了
  • 小心 .setMakeClean,因为它可能会导致
    标签合并,例如。

标签: java html xhtml jtidy


【解决方案1】:

当输入也被视为 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。

【讨论】:

    【解决方案2】:

    我创建了一个函数来解析 xhtml 代码并删除不受欢迎的标签 并添加指向 css 文件“tableStyle.css”的链接

        public static  String xhtmlparser(){ 
        String Cleanline="";
    
        try { 
            // the file url
            FileInputStream fstream = new FileInputStream("c.xhtml");
            // Use DataInputStream to read binary NOT text.
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine = null;
            int linescounter=0;
            while ((strLine = br.readLine()) != null)   {// read every line in the file             
                String m=strLine.replaceAll(" ", "");
                linescounter++;
                if(linescounter==5)
                    m=m+"\n"+ "<link rel="+ "\"stylesheet\" "+"type="+ "\"text/css\" "+"href= " +"\"tableStyle.css\""+ "/>";
                Cleanline+=m+"\n";
            }
    
        }
        catch(IOException e){}
    
        return Cleanline;
    }
    

    但作为性能问题,它好吗?

    它的工作方式会

    【讨论】:

    • 不应该是  实体被转换为  ?
    【解决方案3】:

    可以使用以下方法从html中获取xhtml

    public static String getXHTMLFromHTML(String inputFile,
                String outputFile) throws Exception {
    
            File file = new File(inputFile);
            FileOutputStream fos = null;
            InputStream is = null;
            try {
                fos = new FileOutputStream(outputFile);
                is = new FileInputStream(file);
                Tidy tidy = new Tidy(); 
                tidy.setXHTML(true); 
                tidy.parse(is, fos);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally{
                if(fos != null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        fos = null;
                    }
                    fos = null;
                }
                if(is != null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        is = null;
                    }
                    is = null;
                }
            }
    
            return outputFile;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 2012-09-08
      • 2011-08-21
      • 1970-01-01
      • 2011-10-15
      • 2012-03-17
      • 2014-04-18
      相关资源
      最近更新 更多