Class ElementQuery
java.lang.Object
eu.maveniverse.domtrip.ElementQuery
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 Summary
Modifier and TypeMethodDescriptionall()Returns all elements matching the query criteria as a Stream.atDepth(int depth) Filters elements at the specified depth from the root element.containingText(String text) Filters elements that contain the specified text.longcount()Counts the number of elements matching the query criteria.booleanexists()Checks if any elements match the query criteria.first()Returns the first element matching the query criteria.toList()Returns all elements matching the query criteria as a List.Applies a custom filter predicate.withAttribute(QName attributeQName) Filters elements that have the specified QName attribute.withAttribute(QName attributeQName, String attributeValue) Filters elements that have the specified QName attribute with the given value.withAttribute(String attributeName) Filters elements that have the specified attribute.withAttribute(String attributeName, String attributeValue) Filters elements that have the specified attribute with the given value.Filters elements that have child elements.Filters elements by local name.withNamespace(String namespaceURI) Filters elements by namespace URI.Filters elements that have no child elements.Filters elements by QName (namespace URI and local name).withTextContent(String textContent) Filters elements by text content.
-
Method Details
-
withName
Filters elements by local name.- Parameters:
name- the local name to match- Returns:
- a new ElementQuery with the name filter applied
-
withQName
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
Filters elements by namespace URI.- Parameters:
namespaceURI- the namespace URI to match- Returns:
- a new ElementQuery with the namespace filter applied
-
withAttribute
Filters elements that have the specified attribute.- Parameters:
attributeName- the attribute name- Returns:
- a new ElementQuery with the attribute presence filter applied
-
withAttribute
Filters elements that have the specified attribute with the given value.- Parameters:
attributeName- the attribute nameattributeValue- the attribute value to match- Returns:
- a new ElementQuery with the attribute value filter applied
-
withAttribute
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
Filters elements that have the specified QName attribute with the given value.- Parameters:
attributeQName- the attribute QNameattributeValue- the attribute value to match- Returns:
- a new ElementQuery with the QName attribute value filter applied
-
withTextContent
Filters elements by text content.- Parameters:
textContent- the text content to match- Returns:
- a new ElementQuery with the text content filter applied
-
containingText
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
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
Filters elements that have child elements.- Returns:
- a new ElementQuery with the has children filter applied
-
withoutChildren
Filters elements that have no child elements.- Returns:
- a new ElementQuery with the no children filter applied
-
where
Applies a custom filter predicate.- Parameters:
customFilter- the custom filter predicate- Returns:
- a new ElementQuery with the custom filter applied
-
first
-
all
-
toList
-
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
-