Package org.shredzone.commons.xml
Class XQuery
- java.lang.Object
-
- org.shredzone.commons.xml.XQuery
-
@ParametersAreNonnullByDefault @Immutable @ThreadSafe public class XQuery extends Object
Helps to easily read content from XML sources.A main goal of
XQueryis to keep XML reading as simple as possible. For this reason, sophisticated XML features like validation or namespaces are not supported.Performance was not a goal as well. If you need to parse large documents, you better use the old-fashioned Java ways.
- Author:
- Richard "Shred" Körber
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StringallText()Returns the text content of the entire tree that is spawned by this node.Stream<String>allValue(String xpath)Selects values based on the XPath expression that is applied to the tree represented by thisXQuery.Map<String,String>attr()Returns a map of attributes.booleanexists(String xpath)Checks if there is at least one element matching the XPath expression.XQueryget(String xpath)Gets a single element based on the XPath expression that is applied to the tree represented by thisXQuery.booleanisRoot()Checks if this is a root node.Stringname()Returns the tag name of this node.Optional<XQuery>nextSibling()Returns the next sibling of this element.Optional<XQuery>parent()Returns the parent node of this node, asXQueryobject.static XQueryparse(InputStream in)Parses an XML source and returns anXQueryobject representing the root of the document.static XQueryparse(Reader r)Parses an XML source and returns anXQueryobject representing the root of the document.static XQueryparse(String xml)Parses an XML source and returns anXQueryobject representing the root of the document.static XQueryparse(InputSource in)Parses an XML source and returns anXQueryobject representing the root of the document.Optional<XQuery>previousSibling()Returns the previous sibling of this element.XQueryroot()Returns the root node of this node, asXQueryobject.Stream<XQuery>select(String xpath)Selects elements based on the XPath expression that is applied to the tree represented by thisXQuery.Stream<XQuery>stream()Streams all children of this element.Stringtext()Returns the text content of this node.Stringtext(String xpath)Returns the text selected by the XPath expression.Stream<String>value(String xpath)Selects values based on the XPath expression that is applied to the tree represented by thisXQuery.
-
-
-
Method Detail
-
parse
@Nonnull public static XQuery parse(@WillClose InputSource in) throws IOException
Parses an XML source and returns anXQueryobject representing the root of the document.- Parameters:
in-InputSourceof the XML document- Returns:
XQueryrepresenting the root of the parsed document- Throws:
IOException- if the XML source could not be read or parsed for any reason
-
parse
@Nonnull public static XQuery parse(@WillClose InputStream in) throws IOException
Parses an XML source and returns anXQueryobject representing the root of the document.- Parameters:
in-InputStreamof the XML document- Returns:
XQueryrepresenting the root of the parsed document- Throws:
IOException- if the XML source could not be read or parsed for any reason
-
parse
@Nonnull public static XQuery parse(@WillClose Reader r) throws IOException
Parses an XML source and returns anXQueryobject representing the root of the document.- Parameters:
r-Readerproviding the XML document- Returns:
XQueryrepresenting the root of the parsed document- Throws:
IOException- if the XML source could not be read or parsed for any reason
-
parse
@Nonnull public static XQuery parse(String xml) throws IOException
Parses an XML source and returns anXQueryobject representing the root of the document.- Parameters:
xml- String containing the XML document- Returns:
XQueryrepresenting the root of the parsed document- Throws:
IOException- if the XML source could not be read or parsed for any reason
-
stream
@Nonnull public Stream<XQuery> stream()
Streams all children of this element. Children elements are represented byXQueryobjects as well.- Returns:
Streamof children
-
nextSibling
@Nonnull public Optional<XQuery> nextSibling()
Returns the next sibling of this element.- Returns:
- Next sibling element
- Since:
- 1.1
-
previousSibling
@Nonnull public Optional<XQuery> previousSibling()
Returns the previous sibling of this element.- Returns:
- Previous sibling element
- Since:
- 1.1
-
select
@Nonnull public Stream<XQuery> select(String xpath)
Selects elements based on the XPath expression that is applied to the tree represented by thisXQuery.- Parameters:
xpath- XPath expression- Returns:
- Stream of selected nodes as
XQueryobject
-
get
@Nonnull public XQuery get(String xpath)
Gets a single element based on the XPath expression that is applied to the tree represented by thisXQuery. Exactly one element is expected to match the XPath expression, otherwise an exception is thrown.- Parameters:
xpath- XPath expression- Returns:
- Selected node
- Since:
- 1.1
-
exists
public boolean exists(String xpath)
Checks if there is at least one element matching the XPath expression.- Parameters:
xpath- XPath expression- Returns:
trueif there is at least one element,falseif there is none.- Since:
- 1.1
-
value
@Nonnull public Stream<String> value(String xpath)
Selects values based on the XPath expression that is applied to the tree represented by thisXQuery.- Parameters:
xpath- XPath expression- Returns:
- Stream of strings containing the node values
-
allValue
@Nonnull public Stream<String> allValue(String xpath)
Selects values based on the XPath expression that is applied to the tree represented by thisXQuery. In contrast tovalue(String), this method reads the element texts recursively, usingallText().- Parameters:
xpath- XPath expression- Returns:
- Stream of strings containing the node values
-
text
@Nonnull public String text(String xpath)
Returns the text selected by the XPath expression.- Parameters:
xpath- XPath expression- Returns:
- Text selected by the expression
-
name
@Nonnull public String name()
Returns the tag name of this node.- Returns:
- this
XQuerynode's tag name.
-
text
@Nonnull public String text()
Returns the text content of this node.- Returns:
- this
XQuerynode's text content, non recursively.
-
allText
@Nonnull public String allText()
Returns the text content of the entire tree that is spawned by this node.- Returns:
- this
XQuerynode's text content, recursively.
-
attr
@Nonnull public Map<String,String> attr()
Returns a map of attributes.- Returns:
- a map of this node's attributes.
-
parent
@Nonnull public Optional<XQuery> parent()
Returns the parent node of this node, asXQueryobject. A root node returns an empty optional instead.- Returns:
- parent node
-
isRoot
public boolean isRoot()
Checks if this is a root node.- Returns:
trueif this is a root node,falseif there's a parent.- Since:
- 1.1
-
-