Example usage for org.jsoup.nodes Element tagName

List of usage examples for org.jsoup.nodes Element tagName

Introduction

In this page you can find the example usage for org.jsoup.nodes Element tagName.

Prototype

public String tagName() 

Source Link

Document

Get the name of the tag for this element.

Usage

From source file:xxx.web.comments.debates.impl.ProConOrgParser.java

/**
 * Extracts the document of the quote//  w w  w . j  a  va2 s  .  co m
 *
 * @param textElement text quote element
 * @return plain string with paragraphs kept
 */
public static String extractPlainTextFromTextElement(Element textElement) {
    StringBuilder sb = new StringBuilder();

    for (Node childNode : textElement.childNodes()) {
        if (childNode instanceof Element) {
            Element childElement = (Element) childNode;

            String tagName = childElement.tagName();

            if ("p".equals(tagName) || "span".equals(tagName)) {
                sb.append(childElement.text());
                sb.append("\n");
            } else if ("br".equals(tagName)) {
                // prevent double newlines
                sb = new StringBuilder(sb.toString().trim());
                sb.append("\n");
            }

        } else if (childNode instanceof TextNode) {
            TextNode textNode = (TextNode) childNode;

            sb.append(textNode.text());
        }
    }

    // remove leading + ending quotes
    return Utils.normalize(sb.toString()).replaceAll("[(^\")(\"$)]", "");
}