Class XQuery


  • @ParametersAreNonnullByDefault
    @Immutable
    @ThreadSafe
    public class XQuery
    extends Object
    Helps to easily read content from XML sources.

    A main goal of XQuery is 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 Detail

      • parse

        @Nonnull
        public static XQuery parse​(String xml)
                            throws IOException
        Parses an XML source and returns an XQuery object representing the root of the document.
        Parameters:
        xml - String containing the XML document
        Returns:
        XQuery representing the root of the parsed document
        Throws:
        IOException - if the XML source could not be read or parsed for any reason
      • select

        @Nonnull
        public Stream<XQueryselect​(String xpath)
        Selects elements based on the XPath expression that is applied to the tree represented by this XQuery.
        Parameters:
        xpath - XPath expression
        Returns:
        Stream of selected nodes as XQuery object
      • get

        @Nonnull
        public XQuery get​(String xpath)
        Gets a single element based on the XPath expression that is applied to the tree represented by this XQuery. 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:
        true if there is at least one element, false if there is none.
        Since:
        1.1
      • value

        @Nonnull
        public Stream<Stringvalue​(String xpath)
        Selects values based on the XPath expression that is applied to the tree represented by this XQuery.
        Parameters:
        xpath - XPath expression
        Returns:
        Stream of strings containing the node values
      • allValue

        @Nonnull
        public Stream<StringallValue​(String xpath)
        Selects values based on the XPath expression that is applied to the tree represented by this XQuery. In contrast to value(String), this method reads the element texts recursively, using allText().
        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
      • text

        @Nonnull
        public String text()
        Returns the text content of this node.
        Returns:
        this XQuery node'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 XQuery node's text content, recursively.
      • parent

        @Nonnull
        public Optional<XQueryparent()
        Returns the parent node of this node, as XQuery object. A root node returns an empty optional instead.
        Returns:
        parent node
      • isRoot

        public boolean isRoot()
        Checks if this is a root node.
        Returns:
        true if this is a root node, false if there's a parent.
        Since:
        1.1
      • root

        @Nonnull
        public XQuery root()
        Returns the root node of this node, as XQuery object. A root node returns itself.
        Returns:
        root node
        Since:
        1.1