下面列出了org.jsoup.nodes.Node#parent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
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;
}
/**
* 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();
}
}
}
/**
* 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");
}
/**
* 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;
}
/**
* 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;
}
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;
}
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;
}