Один из способов очистки HTML-тегов от тела с помощью XSLT: использование Tidy из org.w3c
например,
<xsl:template match="/">
<document>
<xsl:apply-templates match="h2[@class='entry-title']" />
<xsl:for-each select="html/head/meta">
<xsl:if test="@name != '' and @content != 'null'">
<content>
<xsl:attribute name="name">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:value-of select="@content" />
</content>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates match="div[@class='entry-content']" />
</document>
<xsl:apply-templates select="ul[@class='sidebar grid-25']" />
</xsl:template>
<xsl:template match="h2[@class='entry-title']">
<content name="title">
<xsl:value-of select="htmlparser:parseHTMLtoDocument(.)" />
</content>
</xsl:template>
<xsl:template match="div[@class='entry-content']">
<content name="snippet" weight="1" output-action="summarize" type="html">
<xsl:value-of select="htmlparser:parseHTMLtoDocument(.)" />
</content>
</xsl:template>
<xsl:template match="ul[@class='sidebar grid-25']">
<xsl:value-of select="htmlparser:parseHTMLtoDocument(.)" />
</xsl:template>
Вы можете создать класс с именем com.xyz.commons.xsl.HtmlDocumentParser , как показано ниже, и вызвать его метод:
public class HtmlDocumentParser {
private static Logger log = Logger.getLogger(HtmlDocumentParser.class);
private static Log4jPrintWriter log4j = new Log4jPrintWriter(log, Level.WARN);
public static Document parseHTMLtoDocument(final String input) {
return parseHTMLtoDocument(input, "UTF-8");
}
public static Document parseHTMLtoDocument(final String input, final String encoding) {
final String htmlInput = String
.format("<!DOCTYPE HTML><html>\n<head>\n<title>\n</title>\n</head>\n<body>\n%s</body></html>", input);
Tidy tidy = new Tidy();
tidy.setInputEncoding(encoding);
tidy.setOutputEncoding(encoding);
tidy.setXHTML(true);
tidy.setXmlOut(true);
tidy.setEncloseBlockText(true);
tidy.setEncloseText(true);
tidy.setMakeBare(true);
tidy.setMakeClean(true);
tidy.setWord2000(true);
tidy.setDropFontTags(true);
tidy.setQuiet(true);
tidy.setErrout(log4j);
Document doc = tidy.parseDOM(new ByteArrayInputStream(htmlInput.getBytes(Charset.forName(encoding))), null);
return doc;
}}