Class ElementQuery

java.lang.Object
eu.maveniverse.domtrip.ElementQuery

public class ElementQuery extends Object
Fluent API for querying and filtering XML elements.

ElementQuery provides a powerful, chainable interface for finding elements based on various criteria including name, namespace, attributes, text content, and structural relationships. It supports both immediate and lazy evaluation of query results.

Usage Examples:

// Find all test dependencies
List<Element> testDeps = root.query()
    .withName("dependency")
    .withAttribute("scope", "test")
    .collect(Collectors.toList());

// Find first element in specific namespace
Optional<Element> soapBody = root.query()
    .withQName(QName.of("http://schemas.xmlsoap.org/soap/envelope/", "Body"))
    .first();

// Complex query with multiple criteria
Stream<Element> results = root.query()
    .withNamespace("http://maven.apache.org/POM/4.0.0")
    .withTextContent("junit")
    .atDepth(3)
    .all();

Query Types:

  • Name-based - Filter by element name or QName
  • Attribute-based - Filter by attribute presence or value
  • Content-based - Filter by text content
  • Structural - Filter by depth or position
  • Namespace-based - Filter by namespace URI
See Also:
  • Method Details

    • withName

      public ElementQuery withName(String name)
      Filters elements by local name.
      Parameters:
      name - the local name to match
      Returns:
      a new ElementQuery with the name filter applied
    • withQName

      public ElementQuery withQName(QName qname)
      Filters elements by QName (namespace URI and local name).
      Parameters:
      qname - the QName to match
      Returns:
      a new ElementQuery with the QName filter applied
    • withNamespace

      public ElementQuery withNamespace(String namespaceURI)
      Filters elements by namespace URI.
      Parameters:
      namespaceURI - the namespace URI to match
      Returns:
      a new ElementQuery with the namespace filter applied
    • withAttribute

      public ElementQuery withAttribute(String attributeName)
      Filters elements that have the specified attribute.
      Parameters:
      attributeName - the attribute name
      Returns:
      a new ElementQuery with the attribute presence filter applied
    • withAttribute

      public ElementQuery withAttribute(String attributeName, String attributeValue)
      Filters elements that have the specified attribute with the given value.
      Parameters:
      attributeName - the attribute name
      attributeValue - the attribute value to match
      Returns:
      a new ElementQuery with the attribute value filter applied
    • withAttribute

      public ElementQuery withAttribute(QName attributeQName)
      Filters elements that have the specified QName attribute.
      Parameters:
      attributeQName - the attribute QName
      Returns:
      a new ElementQuery with the QName attribute presence filter applied
    • withAttribute

      public ElementQuery withAttribute(QName attributeQName, String attributeValue)
      Filters elements that have the specified QName attribute with the given value.
      Parameters:
      attributeQName - the attribute QName
      attributeValue - the attribute value to match
      Returns:
      a new ElementQuery with the QName attribute value filter applied
    • withTextContent

      public ElementQuery withTextContent(String textContent)
      Filters elements by text content.
      Parameters:
      textContent - the text content to match
      Returns:
      a new ElementQuery with the text content filter applied
    • containingText

      public ElementQuery containingText(String text)
      Filters elements that contain the specified text.
      Parameters:
      text - the text to search for
      Returns:
      a new ElementQuery with the text contains filter applied
    • atDepth

      public ElementQuery atDepth(int depth)
      Filters elements at the specified depth from the root element.
      Parameters:
      depth - the depth level (0 = root element, 1 = direct children, etc.)
      Returns:
      a new ElementQuery with the depth filter applied
    • withChildren

      public ElementQuery withChildren()
      Filters elements that have child elements.
      Returns:
      a new ElementQuery with the has children filter applied
    • withoutChildren

      public ElementQuery withoutChildren()
      Filters elements that have no child elements.
      Returns:
      a new ElementQuery with the no children filter applied
    • where

      public ElementQuery where(Predicate<Element> customFilter)
      Applies a custom filter predicate.
      Parameters:
      customFilter - the custom filter predicate
      Returns:
      a new ElementQuery with the custom filter applied
    • first

      public Optional<Element> first()
      Returns the first element matching the query criteria.
      Returns:
      an Optional containing the first matching element, or empty if none found
    • all

      public Stream<Element> all()
      Returns all elements matching the query criteria as a Stream.
      Returns:
      a Stream of matching elements
    • toList

      public List<Element> toList()
      Returns all elements matching the query criteria as a List.
      Returns:
      a List of matching elements
    • count

      public long count()
      Counts the number of elements matching the query criteria.
      Returns:
      the count of matching elements
    • exists

      public boolean exists()
      Checks if any elements match the query criteria.
      Returns:
      true if at least one element matches