org.jsoup.nodes.Node#parent ( )源码实例Demo

下面列出了org.jsoup.nodes.Node#parent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: dkpro-c4corpus   文件: NodeHelper.java
/**
 * Returns true if node1 is ancestor of node2 or node1 == node2
 *
 * @param node1 node 1
 * @param node2 node 2
 * @return boolean value
 */
public static boolean isAncestor(Node node1, Node node2)
{
    if (node1 == node2) {
        return true;
    }
    Node ancestor = node2;

    while (ancestor != null) {
        if (ancestor == node1) {
            return true;
        }
        ancestor = ancestor.parent();
    }

    return false;
}
 
源代码2 项目: dkpro-c4corpus   文件: Paragraph.java
public String getPath(Node n)
{
    String nodePath = "";
    while (n != null) {
        if (n instanceof TextNode) {
            n = n.parent();
        }
        if (NodeHelper.isInnerText(n)) {
            n = n.parent();
        }
        String parentNodeName = n.nodeName();
        nodePath = parentNodeName + "." + nodePath;

        if (!parentNodeName.equalsIgnoreCase("html")) {
            n = n.parent();
        }
        else {
            break;
        }
    }

    return nodePath;
}
 
源代码3 项目: jsoup-learning   文件: NodeTraversor.java
/**
 * 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();
        }
    }
}
 
源代码4 项目: dkpro-c4corpus   文件: NodeHelper.java
/**
 * Returns the nearest common ancestor of node1 and node2
 *
 * @param node1 node 1
 * @param node2 node 2
 * @return nearest common ancestor node
 * @throws IllegalStateException if node1 and node2 has no common ancestor
 *                               to make sure that node1 and node2 should inside the same document
 */
public static Node nearestCommonAncestor(Node node1, Node node2)
{
    Node ancestor = node1;
    while (ancestor != null) {
        if (isAncestor(ancestor, node2)) {
            return ancestor;
        }
        ancestor = ancestor.parent();
    }
    throw new IllegalStateException("node1 and node2 do not have common ancestor");
}
 
源代码5 项目: dkpro-c4corpus   文件: NodeHelper.java
/**
 * Returns true if node has a link ancestor
 *
 * @param node node
 * @return boolean value
 */
public static boolean isLink(Node node)
{
    Node ancestor = node;

    while (ancestor != null) {
        if (isLinkTag(ancestor)) {
            return true;
        }
        ancestor = ancestor.parent();
    }

    return false;
}
 
源代码6 项目: dkpro-c4corpus   文件: ParagraphsExplorer.java
/**
 * Visit from node to the ancestor - if all the visited ancestors are
 * {@link NodeHelper.TagType#INNER_TEXT} returns
 * {@link ParagraphsExplorer.AncestorState#INNERTEXT_ONLY} - if one of the
 * visited ancestors is {@link NodeHelper#isBlockTag(Node)}
 * returns {@link ParagraphsExplorer.AncestorState#BLOCKLEVEL} - otherwise
 * returns {@link ParagraphsExplorer.AncestorState#UNKNOW}
 */
private static AncestorState getAncestorStateOfBranch(Node ancestor, Node node)
{
    if (!NodeHelper.isAncestor(ancestor, node)) {
        throw new InvalidParameterException("ancestor pre-condition violation");
    }
    if (node == ancestor) {
        if (NodeHelper.isBlockTag(node)) {
            return AncestorState.BLOCKLEVEL;
        }
        if (NodeHelper.isInlineTag(node)) {
            return AncestorState.INNERTEXT_ONLY;
        }
        return AncestorState.UNKNOW;
    }
    Node n = node.parent();
    boolean innerTextOnly = true;
    while (n != ancestor && n != null) {
        if (NodeHelper.isBlockTag(n)) {
            return AncestorState.BLOCKLEVEL;
        }
        if (!NodeHelper.isInlineTag(n)) {
            innerTextOnly = false;
        }
        n = n.parent();
    }
    return innerTextOnly ? AncestorState.INNERTEXT_ONLY : AncestorState.UNKNOW;
}
 
源代码7 项目: ContentExtractor   文件: JsoupHelper.java
public static String getXpath(Node node) {
    String result = "";
    Node temp = node;
    while (temp != null) {
        String name = getNodeName(temp);
        result = "," + name + result;
        temp = temp.parent();
    }
    return result;
    
}
 
源代码8 项目: WordCount   文件: JsoupHelper.java
public static String getXpath(Node node) {
    String result = "";
    Node temp = node;
    while (temp != null) {
        String name = getNodeName(temp);
        result = "," + name + result;
        temp = temp.parent();
    }
    return result;
    
}