生成嵌套html标记的格式化输出

时间:2019-04-12 08:17:14

标签: java

我需要解析html标签以产生等效的富文本输出。

输入字符串: Also require a pop up <em>window </em>to guide user to the directory folder to directly open the excel file created from <u>the export </u><strong><u>function</u></strong>

我可以成功检测到<em><u><strong>标签,如下所示:

NodeList nlist = doc.getElementsByTagName("*");
for (int i = 0; i < nlist.getLength(); i++) {
   if (nlist.item(i).getNodeName().equalsIgnoreCase("p")) {

      nlistTags = doc.getElementsByTagName("em");
      for (int j = 0; j < nlistTags.getLength(); j++) {
          System.out.println("<em> tag element: " + nlistTags.item(j).getTextContent());
      }
      nlistTags = doc.getElementsByTagName("u");
      for (int j = 0; j < nlistTags.getLength(); j++) {
          System.out.println("<u> tag element: " + nlistTags.item(j).getTextContent());
      }
      nlistTags = doc.getElementsByTagName("strong");
      for (int j = 0; j < nlistTags.getLength(); j++) {
          System.out.println("<strong> tag element: " + nlistTags.item(j).getTextContent());
      }

      // This currently prints the complete line in regFont. 
      // I need to print the detected strings above in the corresponding fonts.
      cell.addElement(getPhrase(innerXml(nlist.item(i)), this.regFont));  
}

我无法弄清楚如何获取上面检测到的字符串的索引,以便可以将它们格式化为所需的字体类型。 请帮忙。

0 个答案:

没有答案