【问题标题】:JTidy output in String instead of Document?字符串而不是文档中的 JTidy 输出?
【发布时间】:2021-03-04 11:08:12
【问题描述】:

我正在尝试使用 JTidy 将 HTML 字符串转换为 XHTML 字符串,然后使用 XMLWorkerHelper 进行解析。请问如何从 Tidy 中获取 String 而不是 Document 的输出?

我的代码是:

Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.setQuiet(true);
tidy.setShowWarnings(false);
                
org.w3c.dom.Document ppout = tidy.parseDOM(new ByteArrayInputStream(activityDtl.getPPDescription().toString().getBytes()), null);
System.out.println("ppout: " + ppout);
              
p6.add(new Chunk("Description:   ", smallBold));
ElementList list1 = XMLWorkerHelper.parseToElementList(ppout, null);

for (Element element : list1) {
    p6.add(element);
    preface6.add(p6);
}

【问题讨论】:

    标签: java jtidy


    【解决方案1】:
    InputStream inputStream = new ByteArrayInputStream 
        (activityDtl.getPPDescription().getBytes("UTF-8"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Tidy tidy = new Tidy();
    tidy.setXHTML(true);
    tidy.setQuiet(true);
    tidy.setShowWarnings(false);
                    
    tidy.parseDOM(inputStream, baos);
    String ppDescription = baos.toString();
                  
    p6.add(new Chunk("Description:   ", smallBold));
    ElementList list1 = XMLWorkerHelper.parseToElementList(ppDescription, null);
    for (Element element : list1) {
        p6.add(element);
    }
    preface6.add(p6);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 2017-05-18
      • 2012-03-16
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 2013-04-30
      相关资源
      最近更新 更多