Jsoup的Node类大全(最新)

一、简介

Node类直接继承Object,实现了Cloneable接口,它是一个抽象类,类声明:public abstract class Node extends Object implements Cloneable

直接已知子类:Comment, DataNode, DocumentType, Element, TextNode, XmlDeclaration

Node是节点的抽象模型。Elements, Documents, Comments等都是节点的实例。


二、构造方法

1、protected Node(String baseUri, Attributes attributes)        创建一个新的节点(Node)对象。

参数:baseUri – base URI

            attributes – attributes (not null, but may be empty)

2、protected Node(String baseUri)   创建一个新的节点(Node)对象。指定了baseUri

3、protected Node()  使用默认构造器创建Node对象。不会创建base uri、子节点、属性。谨慎使用。


三、方法详细

1、public abstract String nodeName()   抽象方法。获取节点名称。

2、public String attr(String attributeKey)  通过键(key)来获取属性的值(value)。另外,此方法可以将相对路劲的url转变为绝对路劲,只要将key加上前缀abs。如:String url = a.attr("abs:href");

3、public Attributes attributes()  获取元素的所有属性。

4、public Node attr(String attributeKey, String attributeValue) 通过键设置属性的值。   参数:attributeKey-属性的键 。  attributeValue – 属性的值。

5、public boolean hasAttr(String attributeKey)  检测元素是否有attributeKey键指定的属性。

6、public Node removeAttr(String attributeKey)   从元素中移除attributeKey键指定的属性。

7、public String baseUri()   获取节点的base Url

8、public void setBaseUri(String baseUri)  更新节点和其子孙的base URI

9、public String absUrl(String attributeKey) 从一个可能是相对路劲的URI属性中获取为绝对路径的URL   如:String absUrl = linkEl.absUrl("href");

如果URL已经是绝对路劲,就直接返回它,否则使用baseUri将其转换为绝对路劲的URL

10、public Node childNode(int index)  根据索引得到其相应子节点。

11、public List<Node> childNodes()  得到所有子节点的集合。此集合不能被修改,即不能添加新的字节点。但是原子节点本身可以被操纵。

12、protected Node[] childNodesAsArray() 返回所有子节点的数组。

13、public Node parent() 得到此节点的父节点。如果没有父节点,则返回null。

14、public Document ownerDocument() 得到该节点相关联的Document对象。

15、public void remove() 从DOM树中移除该节点。如果该节点有子节点,则他们也将都被移除。

16、public Node before(String html)  在该节点前面插入一段指定的html到DOM树中

17、public Node before(Node node)  在该节点前面插入一个指定的节点到DOM树中

18、public Node after(String html)     在该节点后面插入一段指定的html到DOM树中

19、public Node after(Node node)   在该节点后面插入一个指定的节点到DOM树中

20、public Node wrap(String html)   用提供的html包装该节点。

21、public Node unwrap()   移除该节点,但保留它的子节点。即它的子节点将取代它的位置而连在其父节点上。返回的是该节点的第一个子节点。如果没有子节点则返回null。

22、public void replaceWith(Node in) 用提供的节点替代该节点在DOM树中的位置。

23、protected void setParentNode(Node parentNode)   设置该节点的父节点为parentNode

24、protected void replaceChild(Node out, Node in)   替换该节点的某个子节点。out为要被替换的节点。in为用来替换的新节点。

25、protected void removeChild(Node out)  移除该节点的指定子节点(out)

26、protected void addChildren(Node… children)  为该节点增加子节点。

27、protected void addChildren(int index, Node… children)  在指定索引位置上为该节点增加子节点。

28、public List<Node> siblingNodes()  返回该节点的兄弟节点的集合。如果该节点没有父节点,则返回空集合。

29、public Node nextSibling()  得到该节点的下一个兄弟节点。如果该节点已是最后一个节点,则返回null。

30、public Node previousSibling()  得到该节点的上一个兄弟节点。如果该节点是第一个节点,则返回null。

31、public int siblingIndex()  得到该节点在所有兄弟节点集合中的索引,即位置。如果该节点是第一个,则返回0.

32、protected void setSiblingIndex(int siblingIndex)   设置该节点在兄弟节点集合中的位置。

33、public Node traverse(NodeVisitor nodeVisitor)   根据该节点及其子节点进行深度优先的遍历。   参数:nodeVisitor – the visitor callbacks to perform on each node

34、public String outerHtml()   得到该节点的外部html。

35、protected void outerHtml(StringBuilder accum)  得到该节点的外部html。

36、public String toString()  覆盖Object的toString

37、protected void indent(StringBuilder accum, int depth, Document.OutputSettings out)

38、public boolean equals(Object o) 覆盖Object的equals

39、public int hashCode()  覆盖Object的hashCode

40、public Node clone()   深复制克隆一个节点  。覆盖Object的clone

41、protected Node doClone(Node parent)