protected String getText(Element element) { StringBuilder accum = new StringBuilder(); for (Node node : element.childNodes()) { if (node instanceof TextNode) { TextNode textNode = (TextNode) node; accum.append(textNode.text()); } } return accum.toString(); }
/** * Split this text node into two nodes at the specified string offset. After splitting, this node will contain the * original text up to the offset, and will have a new text node sibling containing the text after the offset. * @param offset string offset point to split node at. * @return the newly created text node containing the text after the offset. */ public TextNode splitText(int offset) { final String text = coreValue(); Validate.isTrue(offset >= 0, "Split offset must be not be negative"); Validate.isTrue(offset < text.length(), "Split offset must not be greater than current text length"); String head = text.substring(0, offset); String tail = text.substring(offset); text(head); TextNode tailNode = new TextNode(tail); if (parent() != null) parent().addChildren(siblingIndex()+1, tailNode); return tailNode; }
private boolean processText(FormattingAppendable out, TextNode node) { skip(); if (out.isPreFormatted()) { out.append(prepareText(node.getWholeText(), true)); } else { out.append(prepareText(node.text())); } return true; }
public static String getTextAfterTag(Element ele) { String text = ""; for(TextNode node: ele.textNodes()) { text += node.text(); } return text; }
for (Element el : doc.select("body").select("*")) { for (TextNode node : el.textNodes()) { node.text() )); } }
List<TextNode> bodyTextNode = doc.getElementById("content").textNodes(); String html = ""; for(TextNode txNode:bodyTextNode){ html+=txNode.text(); }
public static String getAttributeValueOfJSoupTextNode(TextNode textNode, String attribute) { if(attribute.equals(HtmlSpecialAttributes.SPECIAL_ATTRIBUTE_TEXT)) return textNode.text(); else return null; }
@Override public void visit(Node node) { if (node instanceof TextNode) { String text = ((TextNode) node).text(); if (StringUtils.isNotBlank(text)) { formattedTextBuilder.append(text).append("\n"); } } } }
@Override public void visit(Node node) { if (node instanceof TextNode) { String text = ((TextNode) node).text(); if (StringUtils.isNotBlank(text)) { formattedTextBuilder.append(text).append("\n"); } } } }
protected String getText(Element element) { StringBuilder accum = new StringBuilder(); for (Node node : element.childNodes()) { if (node instanceof TextNode) { TextNode textNode = (TextNode) node; accum.append(textNode.text()); } } return accum.toString(); }
protected String getText(Element element) { StringBuilder accum = new StringBuilder(); for (Node node : element.childNodes()) { if (node instanceof TextNode) { TextNode textNode = (TextNode) node; accum.append(textNode.text()); } } return accum.toString(); }
final String html = "<p> Hi everyone. This is a <em>dead end.</em> Do not go!</p>"; Document doc = Jsoup.parseBodyFragment(html); // Parse html into a document Element pTag = doc.select("p").first(); // Select the p-element (there's just one) // Text before 'em'-tag TextNode preEM = (TextNode) pTag.childNode(0); preEM.text(preEM.text().replace("This is a", "This is not a")); // Text after 'em'-tag TextNode postEM = (TextNode) pTag.childNode(2); postEM.text("You may go!"); System.out.println(pTag); // Print result
@Override public void process(java.util.Stack<NodeHandler> stack, ContentHandler contentHandler) throws SAXException { String text = isPreserveWhitespace() ? node.getWholeText() : node.text(); contentHandler.characters(text.toCharArray(), 0, text.length()); }
final String html = "<div class=example>Text #1</div> \"Another Text 1\"\n" + "<div class=example>Text #2</div> \"Another Text 2\" "; Document doc = Jsoup.parse(html); for( Element element : doc.select("div.example") ) // Select all the div tags { TextNode next = (TextNode) element.nextSibling(); // Get the next node of each div as a TextNode System.out.println(next.text()); // Print the text of the TextNode }
private static int calculateLength(Element el, int depth) { int result = 0; for(final Node n : el.childNodes()) { if(n instanceof Element) { result += (4 * depth) + calculateLength((Element)n, depth+1); } else if(n instanceof TextNode) { result += ((TextNode)n).text().length(); } } return result; }
@Override public String getAttribute(String attributeKey) { if(attributeKey.equals(HtmlSpecialAttributes.SPECIAL_ATTRIBUTE_TEXT)) return getJsoupTextNode().text(); else return super.getAttribute(attributeKey); }
@Override public void handleTextNode(TextNode node, DocumentConverter converter) { // Override to provide special handling for ignoring // leading or trailing all-space nodes. if((node.previousSibling() != null && node.nextSibling() != null) || node.text().trim().length() != 0) { super.handleTextNode(node, converter); } }
public void head(Node node, int depth) { if (node.getClass().equals(TextNode.class)) { if (this.preserveWhitespace) builder.add(((TextNode) node).getWholeText()); else builder.add(((TextNode) node).text()); } else { beginMap.put(node, builder.getPosition()); } }
@Override public void head(Node node, int depth) { if (node instanceof TextNode) { if (this.preserveWhitespace) builder.add(((TextNode) node).getWholeText()); else builder.add(((TextNode) node).text()); } else { beginMap.put(node, builder.getPosition()); } }