Element class of jsoup

Source: Internet
Author: User
Tags baseuri

I. Introduction

This class is a direct subclass of Node and also implements the clone interface. Class Declaration: public classElementExtends Node

It represents an html element consisting of a tag name, multiple attributes, and subnodes. From this element, you can extract data, traverse the node tree, and manipulate html.

Ii. Constructor

1. publicElement(Tag tag, String baseUri, Attributes attributes) to create a new, independent element. Independent: no parent node. Attributes indicates the initial attribute.

2. publicElement(Tag tag, String baseUri) use tags and base URLs to create a new element.

Iii. Detailed Method

1. public StringNodeName() Get the node name

2. public StringTagName() Obtain the Tag Name of the element, such as div.

3. public ElementTagName(String tagName) to change the label of an element. For example, el. tagName ("div ")The label is changed to a label.

4. public TagTag() Obtain the Tag of the element.

5. public booleanIsBlock() Test whether the element is a block-level element.

6. public StringId() Obtain the element id attribute.

7. public ElementAttr(String attributeKey, String attributeValue) sets the attribute value of an element. If the key already exists, the previous value is replaced; otherwise, the key is added.

8. public Map Dataset() Obtain the HTML5 Custom Data Attribute of the element. The attribute of each key starting with "data-" in the element is included in Dataset.

For example, this element... has the following Dataset:package=jsoup, language=java.

The returned map is the filtered map set of element attributes. Changes to a map (add, delete, or modify) will affect other maps.

9. public final ElementParent() Obtain the parent node of the node

10. public ElementsParents() Obtain the parent class and ancestor node of the element until the root of the document. Returns the stack of a parent class whose elements are closest to each other.

11. public ElementChild(Int index) obtain the child element of an element through the index.

Note: A child element of an element can be a mixture of elements and nodes. This method only checks the set of child elements after filtering (retain the children whose children are elements, and filter out the children whose children is Nodes). The index is also based on the index of the filtered set.

12. public ElementsChildren() Obtain the sub-element set

13. public List TextNodes() To obtain the child text node set of the element. The set cannot be modified, but the text node can be manipulated. This is a more efficient way to filter text nodes.

For example, this section of html:

One Two Three
Four

Use the p element for selection.

p.text()= "One Two Three Four" p.ownText()= "One Three Four" p.children()= Elements[,
]
p.childNodes()= List ["One ", , " Three ",
, " Four"]
p.textNodes()= List ["One ", " Three ", " Four"]

14. public List DataNodes() Obtain the child data node of the element. This set cannot be modified, but data nodes can be manipulated. This is a more efficient way to filter data nodes.

15. public ElementsSelect(String cssQuery) query the element set that matches the CSS query selector. The matched element may be itself or any child element of it. This method is generally more powerful than the method that uses the DOM type getelementby *, because multiple filters can be combined. For example:

el.select("a[href]")-Finds links ( aTags hrefAttributes) el.select("a[href*=example.com]")-Finds links pointing to example.com (loosely)

16. public ElementAppendChild(Node child) adds a subnode to the element. It is required that the child node does not already have a parent class.

17. public ElementPrependChild(Node child) Add a child Node at the beginning of the child elements of the element. It is required that the child node does not already have a parent class.

18. public ElementAppendElement(String tagName) Use tagName to create a new element and use it as the last child element of the element. For example, parent. appendElement ("h1"). attr ("id", "header"). text ("Welcome ");

19. public ElementPrependElement(String tagName) creates a new element and uses it as the first child element of the element.

20. public ElementAppendText(String text) create a new text node and append it to the element.

21. public ElementPrependText(String text) creates a new text node and places it at the beginning of the child element of the element.

22. public ElementAppend(String html) Add a piece of html to the element. The html will be parsed and each node will be placed at the end of the element.

23. public ElementPrepend(String html) Add a piece of html to this element, the html will be parsed, and each node will be placed at the beginning of the element.

24. public ElementBefore(String html) insert a specified html to the DOM tree before the element. For example, it is used as a sibling node.

25. public ElementBefore(Node node) insert a specified Node to the DOM tree before the node. For example, it is used as a sibling node.

26. public ElementAfter(String html) insert a specified html to the DOM tree behind the element. For example, it is used as a sibling node.

27. public ElementAfter(Node node) insert a specified Node to the DOM tree behind the node. For example, it is used as a sibling node.

28. public ElementEmpty() Remove all child nodes of the element.

29. public ElementWrap(String html) wrap the element with the supplied html.

30. public ElementsSiblingElements() Obtain the sibling element of the element. This element is not included.

31. public ElementNextElementSibling() To obtain the next sibling element of the element.

32. public ElementPreviuselementsibling() Obtain the previous sibling element of the element.

33. public ElementFirstElementSibling() Obtain the first sibling element of the element.

34. public IntegerElementSiblingIndex() Obtain the index of this element in the sibling element set. If this element is the first one, 0 is returned.

35. public ElementLastElementSibling() Obtain the last sibling element of the element.

36. public ElementsGetElementsByTag(String tagName) query sub-element set by tagName

37. public ElementGetElementById(String id) Search for elements by ID. Both the element itself and its subelements are in the query range.

Note: This method is used to find the first matched ID element starting from this element. If you use different locations as the starting point to find different elements that match this ID

38. public ElementsGetElementsByClass(String className) to find the Element Set of the class containing className. Both the element itself and its subelements are in the query range. Case Insensitive. The element set may contain multiple classes (). This method detects each class, so you can use el. getElementsByClass ("header") to find the above element.

39. public ElementsGetElementsByAttribute(String key) searches for the element set by the key of the element attribute.

40. public ElementsGetElementsByAttributeStarting(String keyPrefix) searches for elements based on the attribute prefix. HTML5 attribute set usagedata-Prefix

41. public ElementsGetElementsByAttributeValue(String key, String value. Case Insensitive.

42. public ElementsGetElementsByAttributeValueNot(String key, String value) searches for element sets that do not contain the specified attribute or contain but have different values. Case Insensitive.

43. public ElementsGetElementsByAttributeValueStarting(String key, String valuePrefix) Find the element set whose key is key and whose value starts with valuePrefix.

44. public ElementsGetElementsByAttributeValueEnding(String key, String valueSuffix) Find the element set with the key as the key and the value valueSuffix as the suffix.

45. public ElementsGetElementsByAttributeValueContaining(String key, String match) The search key is key, and the value contains the element set of match.

46. public ElementsGetElementsByAttributeValueMatching(String key, Pattern pattern) The search key is key, and the value matches the Element Set of the given regular expression.

47. public ElementsGetElementsByAttributeValueMatching(String key, String regex) finds the element set with the key and the value matches the given regular expression.

48. public ElementsGetElementsByIndexLessThan(Int index) Find the sibling element set whose index is smaller than the index.

49. public ElementsGetElementsByIndexGreaterThan(Int index) Find the sibling element set with the index greater than the index.

50. public ElementsGetElementsByIndexEquals(Int index) Find the sibling element set whose index is equal to the index.

51. public ElementsGetElementsContainingText(String searchText) Search for the element set that contains the searchText String. Case Insensitive. The text may appear directly in the element or its child element.

52. public ElementsGetElementsContainingOwnText(String searchText) Search for the element set that contains the searchText String. Case Insensitive. This text appears in this element. Instead of the child element.

53. public ElementsGetElementsMatchingText(Pattern pattern) searches for the element set in which the text matches the given regular expression.

54. public ElementsGetElementsMatchingText(String regex) searches for the element set in which the text matches the given regular expression.

55. public ElementsGetElementsMatchingOwnText(Pattern pattern) searches for the element set in which the text matches the given regular expression.

56. public ElementsGetElementsMatchingOwnText(String regex) searches for the element set in which the text matches the given regular expression.

57. public ElementsGetAllElements() Search for all element sets, including themselves and their children.

58. public StringText() Get the combination of the element text and its Child text. For example:

HelloThereNow!

P. text () will return "Hello there now! "

59. public StringOwnText() To obtain the text of the element. For example:

HelloThereNow!

P. ownText () will return "Hello now! "

60. public ElementText(String text) set the text content of the element. Any existing text will be cleared.

61. public booleanHasText() Test whether the element contains text without spaces.

62. public StringData() Get the combination of element data. The data can be in the script.

63. public StringClassName() The text of the element class attribute may contain multiple class names, separated by spaces. For example;Return"header gray".

64. public Set ClassNames() Get all class names of the element, for example;, return Set of "header", "gray"

65. public ElementClassNames(Set ClassNames) uses the provided class names to set the class attribute of the element.

66. public booleanHasClass(String className) whether the test element contains a specified class, case insensitive.

67. public ElementAddClass(String className) adds a className value to the class attribute of the element.

68. public ElementRemoveClass(String className) removes the specified value from the class attribute of the element.

69. public ElementToggleClass(String className) reverses the className value of the class attribute. If yes, It is removed. If no, it is added.

70. public StringVal() Obtain the value of the form element.

71. public ElementVal(String value) set the value of the form Element

72. public StringHtml() Retrieving the internal html of an element

73. public ElementHtml(String html) to set the internal html of the element, the existing html will be cleared first.

For more information, see the author and source.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.