How to do HTML to XML conversion to generate closed tags?

后端 未结 1 698
一整个雨季
一整个雨季 2020-12-07 06:34

How to do xml to html conversion to generate closed tags.

The context is explained here: Error while generating pdf from Html file in Java using iText

When I

相关标签:
1条回答
  • 2020-12-07 07:00

    You are experiencing this problem because you are feeding HTML to iText's XML Worker. XML Worker requires XML, so you need to convert your HTML into XHTML.

    There is an example on how to do this on the official iText site: D00_XHTML

    public static void tidyUp(String path) throws IOException {
        File html = new File(path);
        byte[] xhtml = Jsoup.parse(html, "US-ASCII").html().getBytes();
        File dir = new File("results/xml");
        dir.mkdirs();
        FileOutputStream fos = new FileOutputStream(new File(dir, html.getName()));
        fos.write(xhtml);
        fos.close();
    }
    

    In this example, we get a path to an ordinary HTML file (similar to what you have). We then use the Jsoup library to parse the HTML into an XHTML byte array. In this example, we use that byte array to write an XHTML file to disk. You can use the byte array directly as input for XML Worker.

    0 讨论(0)
提交回复
热议问题