HTMLUtils { public static void stripHTMLEnvelope(Document document) { org.w3c.dom.Element root = document.getDocumentElement(); if (root.getNodeName().equalsIgnoreCase(HTMLConstants.TAG_HTML)) { Node bodyNode = null; Node headNode = null; NodeList nodes = root.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeName().equalsIgnoreCase(HTMLConstants.TAG_HEAD)) { headNode = node; } else if (node.getNodeName().equalsIgnoreCase(HTMLConstants.TAG_BODY)) { bodyNode = node; } } if (headNode != null) { root.removeChild(headNode); } if (bodyNode != null) { NodeList bodyChildrenNodes = bodyNode.getChildNodes(); while (bodyChildrenNodes.getLength() > 0) { root.insertBefore(bodyChildrenNodes.item(0), null); } root.removeChild(bodyNode); } } } private  HTMLUtils(); static String toString(Document document); static String toString(Document document, boolean omitDeclaration, boolean omitDoctype); static void stripHTMLEnvelope(Document document); static void stripFirstElementInside(Document document, String parentTagName, String elementTagName); @Unstable static String escapeElementText(String content);  }
@Test void stripHTMLEnvelope() { Document document = this.cleaner.clean(new StringReader("<html><head><body><p>test1</p><p>test2</p></body></html>")); HTMLUtils.stripHTMLEnvelope(document); assertEquals(DefaultHTMLCleanerTest.HEADER + "<html><p>test1</p><p>test2</p></html>\n", HTMLUtils.toString(document)); }