Java Code Examples for org.jsoup.nodes.Node#nextSibling()

The following examples show how to use org.jsoup.nodes.Node#nextSibling() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NodeTraversor.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Start a depth-first traverse of the root and all of its descendants.
 * @param root the root node point to traverse.
 */
public void traverse(Node root) {
    Node node = root;
    int depth = 0;
    
    while (node != null) {
        visitor.head(node, depth);
        if (node.childNodeSize() > 0) {
            node = node.childNode(0);
            depth++;
        } else {
            while (node.nextSibling() == null && depth > 0) {
                visitor.tail(node, depth);
                node = node.parentNode();
                depth--;
            }
            visitor.tail(node, depth);
            if (node == root)
                break;
            node = node.nextSibling();
        }
    }
}
 
Example 2
Source File: NodeTraversor.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Start a depth-first traverse of the root and all of its descendants.
 * @param visitor Node visitor.
 * @param root the root node point to traverse.
 */
public static void traverse(NodeVisitor visitor, Node root) {
    Node node = root;
    int depth = 0;
    
    while (node != null) {
        visitor.head(node, depth);
        if (node.childNodeSize() > 0) {
            node = node.childNode(0);
            depth++;
        } else {
            while (node.nextSibling() == null && depth > 0) {
                visitor.tail(node, depth);
                node = node.parentNode();
                depth--;
            }
            visitor.tail(node, depth);
            if (node == root)
                break;
            node = node.nextSibling();
        }
    }
}
 
Example 3
Source File: NodeTraversor.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Start a depth-first traverse of the root and all of its descendants.
 * @param visitor Node visitor.
 * @param root the root node point to traverse.
 */
public static void traverse(NodeVisitor visitor, Node root) {
    Node node = root;
    int depth = 0;
    
    while (node != null) {
        visitor.head(node, depth);
        if (node.childNodeSize() > 0) {
            node = node.childNode(0);
            depth++;
        } else {
            while (node.nextSibling() == null && depth > 0) {
                visitor.tail(node, depth);
                node = node.parentNode();
                depth--;
            }
            visitor.tail(node, depth);
            if (node == root)
                break;
            node = node.nextSibling();
        }
    }
}
 
Example 4
Source File: NodeTraversor.java    From jsoup-learning with MIT License 6 votes vote down vote up
/**
 * Start a depth-first traverse of the root and all of its descendants.
 * @param root the root node point to traverse.
 */
public void traverse(Node root) {
    Node node = root;
    int depth = 0;
    
    while (node != null) {
        visitor.head(node, depth);
        if (node.childNodeSize() > 0) {
            node = node.childNode(0);
            depth++;
        } else {
            while (node.nextSibling() == null && depth > 0) {
                visitor.tail(node, depth);
                node = node.parent();
                depth--;
            }
            visitor.tail(node, depth);
            if (node == root)
                break;
            node = node.nextSibling();
        }
    }
}
 
Example 5
Source File: HtmlHelper.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
static void convertLists(Document document) {
    for (Element span : document.select("span")) {
        // Skip signature and referenced message
        boolean body = true;
        Element parent = span.parent();
        while (parent != null) {
            if ("div".equals(parent.tagName()) &&
                    !TextUtils.isEmpty(parent.attr("fairemail"))) {
                body = false;
                break;
            }
            parent = parent.parent();
        }
        if (!body)
            continue;

        Element list = null;
        for (int i = 0; i < span.childNodeSize(); i++) {
            boolean item = false;
            Node node = span.childNode(i);
            if (node instanceof TextNode) {
                String text = ((TextNode) node).text().trim();
                Node next = node.nextSibling();
                if ((text.startsWith("* ") || text.startsWith("- ")) &&
                        (next == null || "br".equals(next.nodeName()))) {
                    item = true;
                    String type = (text.startsWith("* ") ? "ul" : "ol");

                    Element li = document.createElement("li");
                    li.text(text.substring(2));

                    if (list == null || !list.tagName().equals(type)) {
                        Node before = node.previousSibling();
                        if (before != null && "br".equals(before.nodeName())) {
                            before.remove();
                            i--;
                        }

                        list = document.createElement(type);
                        list.appendChild(li);
                        node.replaceWith(list);

                    } else {
                        list.appendChild(li);
                        node.remove();
                        i--;
                    }

                    if (next != null)
                        next.remove();
                }
            } else {
                if (list != null && "br".equals(node.nodeName())) {
                    node.remove();
                    i--;
                }
            }
            if (!item)
                list = null;
        }
    }
}