Class ElementQuery


  • public class ElementQuery
    extends java.lang.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:
    Element, QName
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.stream.Stream<Element> all()
      Returns all elements matching the query criteria as a Stream.
      ElementQuery atDepth​(int depth)
      Filters elements at the specified depth from the root element.
      ElementQuery containingText​(java.lang.String text)
      Filters elements that contain the specified text.
      long count()
      Counts the number of elements matching the query criteria.
      boolean exists()
      Checks if any elements match the query criteria.
      java.util.Optional<Element> first()
      Returns the first element matching the query criteria.
      java.util.List<Element> toList()
      Returns all elements matching the query criteria as a List.
      ElementQuery where​(java.util.function.Predicate<Element> customFilter)
      Applies a custom filter predicate.
      ElementQuery withAttribute​(QName attributeQName)
      Filters elements that have the specified QName attribute.
      ElementQuery withAttribute​(QName attributeQName, java.lang.String attributeValue)
      Filters elements that have the specified QName attribute with the given value.
      ElementQuery withAttribute​(java.lang.String attributeName)
      Filters elements that have the specified attribute.
      ElementQuery withAttribute​(java.lang.String attributeName, java.lang.String attributeValue)
      Filters elements that have the specified attribute with the given value.
      ElementQuery withChildren()
      Filters elements that have child elements.
      ElementQuery withName​(java.lang.String name)
      Filters elements by local name.
      ElementQuery withNamespace​(java.lang.String namespaceURI)
      Filters elements by namespace URI.
      ElementQuery withoutChildren()
      Filters elements that have no child elements.
      ElementQuery withQName​(QName qname)
      Filters elements by QName (namespace URI and local name).
      ElementQuery withTextContent​(java.lang.String textContent)
      Filters elements by text content.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • withName

        public ElementQuery withName​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.String attributeName,
                                          java.lang.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,
                                          java.lang.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​(java.lang.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​(java.lang.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​(java.util.function.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 java.util.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 java.util.stream.Stream<Element> all()
        Returns all elements matching the query criteria as a Stream.
        Returns:
        a Stream of matching elements
      • toList

        public java.util.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