Package org.jsoup.select

Class Nodes<T extends Node>

java.lang.Object
java.util.AbstractCollection<T>
java.util.AbstractList<T>
java.util.ArrayList<T>
org.jsoup.select.Nodes<T>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<T>, Collection<T>, List<T>, RandomAccess, SequencedCollection<T>
Direct Known Subclasses:
Elements

public class Nodes<T extends Node> extends ArrayList<T>
A list of Node objects, with methods that act on every node in the list.

Methods that set, remove, or replace nodes in the list will also act on the underlying DOM.

If there are other bulk methods (perhaps from Elements) that would be useful here, please provide feedback.

Since:
1.21.1
See Also:
  • Field Summary

    Fields inherited from class java.util.AbstractList

    modCount
  • Constructor Summary

    Constructors
    Constructor
    Description
    Nodes()
     
    Nodes(int initialCapacity)
     
    Nodes(Collection<T> nodes)
     
    Nodes(List<T> nodes)
     
    Nodes(T... nodes)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Nodes<T>
    after(String html)
    Insert the supplied HTML after each matched nodes's outer HTML.
    ArrayList<T>
    asList()
    Convenience method to get the Nodes as a plain ArrayList.
    Nodes<T>
    before(String html)
    Insert the supplied HTML before each matched node's outer HTML.
    void
    clear()
    Removes all the nodes from this list, and each of them from the DOM.
    Nodes<T>
    clone()
    Creates a deep copy of these nodes.
    T
    deselect(int index)
    Remove the node at the specified index in this list, but not from the DOM.
    boolean
    deselect(Object o)
    Remove the specified node from this list, but not from the DOM.
    void
    Like clear(), removes all the nodes from this list, but not from the DOM.
    @Nullable T
    first()
    Get the first matched element.
    @Nullable T
    last()
    Get the last matched element.
    String
    Get the combined outer HTML of all matched nodes.
    Nodes<T>
    remove()
    Remove each matched node from the DOM.
    T
    remove(int index)
    Remove the node at the specified index in this list, and from the DOM.
    boolean
    remove(Object o)
    Remove the specified node from this list, and from the DOM.
    boolean
    removeAll(Collection<?> c)
    Removes from this list, and from the DOM, each of the nodes that are contained in the specified collection and are in this list.
    boolean
    removeIf(Predicate<? super T> filter)
    Remove from the list, and from the DOM, all nodes in this list that mach the given predicate.
    void
    replaceAll(UnaryOperator<T> operator)
    Replace each node in this list with the result of the operator, and update the DOM.
    boolean
    retainAll(Collection<?> toRemove)
    Retain in this list, and in the DOM, only the nodes that are in the specified collection and are in this list.
    T
    set(int index, T node)
    Replace the node at the specified index in this list, and in the DOM.
    String
    Get the combined outer HTML of all matched nodes.
    Nodes<T>
    wrap(String html)
    Wrap the supplied HTML around each matched node.

    Methods inherited from class java.util.ArrayList

    add, add, addAll, addAll, addFirst, addLast, contains, ensureCapacity, equals, forEach, get, getFirst, getLast, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, removeFirst, removeLast, removeRange, size, sort, spliterator, subList, toArray, toArray, trimToSize

    Methods inherited from class java.util.AbstractCollection

    containsAll

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.util.Collection

    parallelStream, stream, toArray

    Methods inherited from interface java.util.List

    containsAll, reversed
  • Constructor Details

    • Nodes

      public Nodes()
    • Nodes

      public Nodes(int initialCapacity)
    • Nodes

      public Nodes(Collection<T> nodes)
    • Nodes

      public Nodes(List<T> nodes)
    • Nodes

      @SafeVarargs public Nodes(T... nodes)
  • Method Details

    • clone

      public Nodes<T> clone()
      Creates a deep copy of these nodes.
      Overrides:
      clone in class ArrayList<T extends Node>
      Returns:
      a deep copy
    • asList

      public ArrayList<T> asList()
      Convenience method to get the Nodes as a plain ArrayList. This allows modification to the list of nodes without modifying the source Document. I.e. whereas calling nodes.remove(0) will remove the nodes from both the Nodes and the DOM, nodes.asList().remove(0) will remove the node from the list only.

      Each Node is still the same DOM connected Node.

      Returns:
      a new ArrayList containing the nodes in this list
      See Also:
    • remove

      public Nodes<T> remove()
      Remove each matched node from the DOM.

      The nodes will still be retained in this list, in case further processing of them is desired.

      E.g. HTML: <div><p>Hello</p> <p>there</p> <img></div>
      doc.select("p").remove();
      HTML = <div> <img></div>

      Note that this method should not be used to clean user-submitted HTML; rather, use Cleaner to clean HTML.

      Returns:
      this, for chaining
      See Also:
    • outerHtml

      public String outerHtml()
      Get the combined outer HTML of all matched nodes.
      Returns:
      string of all node's outer HTML.
      See Also:
    • toString

      public String toString()
      Get the combined outer HTML of all matched nodes. Alias of outerHtml().
      Overrides:
      toString in class AbstractCollection<T extends Node>
      Returns:
      string of all the node's outer HTML.
      See Also:
    • before

      public Nodes<T> before(String html)
      Insert the supplied HTML before each matched node's outer HTML.
      Parameters:
      html - HTML to insert before each node
      Returns:
      this, for chaining
      See Also:
    • after

      public Nodes<T> after(String html)
      Insert the supplied HTML after each matched nodes's outer HTML.
      Parameters:
      html - HTML to insert after each node
      Returns:
      this, for chaining
      See Also:
    • wrap

      public Nodes<T> wrap(String html)
      Wrap the supplied HTML around each matched node. For example, with HTML <p><b>This</b> is <b>Jsoup</b></p>, doc.select("b").wrap("<i></i>"); becomes <p><i><b>This</b></i> is <i><b>jsoup</b></i></p>
      Parameters:
      html - HTML to wrap around each node, e.g. <div class="head"></div>. Can be arbitrarily deep.
      Returns:
      this (for chaining)
      See Also:
    • first

      public @Nullable T first()
      Get the first matched element.
      Returns:
      The first matched element, or null if contents is empty.
    • last

      public @Nullable T last()
      Get the last matched element.
      Returns:
      The last matched element, or null if contents is empty.
    • set

      public T set(int index, T node)
      Replace the node at the specified index in this list, and in the DOM.
      Specified by:
      set in interface List<T extends Node>
      Overrides:
      set in class ArrayList<T extends Node>
      Parameters:
      index - index of the node to replace
      node - node to be stored at the specified position
      Returns:
      the old Node at this index
    • remove

      public T remove(int index)
      Remove the node at the specified index in this list, and from the DOM.
      Specified by:
      remove in interface List<T extends Node>
      Overrides:
      remove in class ArrayList<T extends Node>
      Parameters:
      index - the index of the node to be removed
      Returns:
      the old node at this index
      See Also:
    • remove

      public boolean remove(Object o)
      Remove the specified node from this list, and from the DOM.
      Specified by:
      remove in interface Collection<T extends Node>
      Specified by:
      remove in interface List<T extends Node>
      Overrides:
      remove in class ArrayList<T extends Node>
      Parameters:
      o - node to be removed from this list, if present
      Returns:
      if this list contained the Node
      See Also:
    • deselect

      public T deselect(int index)
      Remove the node at the specified index in this list, but not from the DOM.
      Parameters:
      index - the index of the node to be removed
      Returns:
      the old node at this index
      See Also:
    • deselect

      public boolean deselect(Object o)
      Remove the specified node from this list, but not from the DOM.
      Parameters:
      o - node to be removed from this list, if present
      Returns:
      if this list contained the Node
      See Also:
    • clear

      public void clear()
      Removes all the nodes from this list, and each of them from the DOM.
      Specified by:
      clear in interface Collection<T extends Node>
      Specified by:
      clear in interface List<T extends Node>
      Overrides:
      clear in class ArrayList<T extends Node>
      See Also:
    • deselectAll

      public void deselectAll()
      Like clear(), removes all the nodes from this list, but not from the DOM.
      See Also:
    • removeAll

      public boolean removeAll(Collection<?> c)
      Removes from this list, and from the DOM, each of the nodes that are contained in the specified collection and are in this list.
      Specified by:
      removeAll in interface Collection<T extends Node>
      Specified by:
      removeAll in interface List<T extends Node>
      Overrides:
      removeAll in class ArrayList<T extends Node>
      Parameters:
      c - collection containing nodes to be removed from this list
      Returns:
      true if nodes were removed from this list
    • retainAll

      public boolean retainAll(Collection<?> toRemove)
      Retain in this list, and in the DOM, only the nodes that are in the specified collection and are in this list. In other words, remove nodes from this list and the DOM any item that is in this list but not in the specified collection.
      Specified by:
      retainAll in interface Collection<T extends Node>
      Specified by:
      retainAll in interface List<T extends Node>
      Overrides:
      retainAll in class ArrayList<T extends Node>
      Parameters:
      toRemove - collection containing nodes to be retained in this list
      Returns:
      true if nodes were removed from this list
      Since:
      1.17.1
    • removeIf

      public boolean removeIf(Predicate<? super T> filter)
      Remove from the list, and from the DOM, all nodes in this list that mach the given predicate.
      Specified by:
      removeIf in interface Collection<T extends Node>
      Overrides:
      removeIf in class ArrayList<T extends Node>
      Parameters:
      filter - a predicate which returns true for nodes to be removed
      Returns:
      true if nodes were removed from this list
    • replaceAll

      public void replaceAll(UnaryOperator<T> operator)
      Replace each node in this list with the result of the operator, and update the DOM.
      Specified by:
      replaceAll in interface List<T extends Node>
      Overrides:
      replaceAll in class ArrayList<T extends Node>
      Parameters:
      operator - the operator to apply to each node