org.appng.api

## Class XPathProcessor

- java.lang.Object
- - org.appng.api.XPathProcessor

-     public class XPathProcessor
      extends Object

  An `XPathProcessor` is used the create a `Document` from an `URL` or `InputStream` and then to extract `NodeList`s, `Node`s, `Element`s, `Attr`ibutes etc. from this `Document`.
  It also allows to create new `Element`s, `Attr`ibutes, `CDATASection`s and `Text`s.
  Author:
    Matthias Müller

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `XPathProcessor(Document document)`
    `XPathProcessor(InputStream is)` Create a new `XPathProcessor` from the given `InputStream`.
    `XPathProcessor(String url)` Create a new `XPathProcessor` from the given URL.
    `XPathProcessor(URL url)` Create a new `XPathProcessor` from the given URL.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Node` | `addAttribute(Node node, String name, String value)` Adds a new `Attr`ibute to the given `Node`.
    `Boolean` | `getBoolean(Node node, String xpathExpression)` Parses the given `xpathExpression` to retrieve a `Boolean`.
    `Boolean` | `getBoolean(String xpathExpression)` Parses the given `xpathExpression` to retrieve a `Boolean`.
    `Document` | `getDocument()` Returns the `Document` parsed from the `URL`/`InputStream`.
    `Element` | `getElement(Node node, String xpathExpression)` Parses the given `xpathExpression` to retrieve an `Element`.
    `Element` | `getElement(String xpathExpression)` Parses the given `xpathExpression` to retrieve an `Element`.
    `Node` | `getNode(Node node, String xpathExpression)` Parses the given `xpathExpression` to retrieve a `Node`.
    `Node` | `getNode(String xpathExpression)` Parses the given `xpathExpression` to retrieve a `Node`.
    `NodeList` | `getNodes(Node node, String xpathExpression)` Parses the given `xpathExpression` to retrieve a `NodeList`.
    `NodeList` | `getNodes(String xpathExpression)` Parses the given `xpathExpression` to retrieve a `NodeList`.
    `Number` | `getNumber(Node node, String xpathExpression)` Parses the given `xpathExpression` to retrieve a `Number`.
    `Number` | `getNumber(String xpathExpression)` Parses the given `xpathExpression` to retrieve a `Number`.
    `String` | `getString(Node node, String xpathExpression)` Parses the given `xpathExpression` to retrieve a `String`.
    `String` | `getString(String xpathExpression)` Parses the given `xpathExpression` to retrieve a `String`.
    `String` | `getXml()` Returns the XML for the `Document` parsed from the `URL`/`InputStream`.
    `String` | `getXml(Node node)` Returns the XML-fragment represented by the given `Node`.
    `String` | `getXml(NodeList nodes)` Returns the XML-fragment represented by the given `NodeList`.
    `void` | `getXml(Node node, OutputStream outputStream)` Writes XML-fragment represented by the given `Node` into the given `OutputStream`.
    `void` | `getXml(OutputStream outputStream)` Writes the XML for the `Document` parsed from the `URL`/`InputStream` into the given `OutputStream`.
    `Attr` | `newAttribute(String name, String value)` Creates a new `Attr`ibute.
    `CDATASection` | `newCDATA(String tagName)` Creates a new `CDATASection`.
    `Element` | `newElement(String tagName)` Creates a new `Element`.
    `Text` | `newText(String tagName)` Creates a new `Text`.
    `void` | `setNamespace(String prefix, String namespace)` Sets the namespace and the prefix to use for that namespace. Example:

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### XPathProcessor

          public XPathProcessor(String url)
                         throws IOException

      Create a new `XPathProcessor` from the given URL.
      Parameters:
        `url` - the URL
      Throws:
        `IOException` - if an error occurs while reading the XML-document from the URL

    - #### XPathProcessor

          public XPathProcessor(URL url)
                         throws IOException

      Create a new `XPathProcessor` from the given URL.
      Parameters:
        `url` - the URL
      Throws:
        `IOException` - if an error occurs while reading the XML-document from the `URL`

    - #### XPathProcessor

          public XPathProcessor(InputStream is)
                         throws IOException

      Create a new `XPathProcessor` from the given `InputStream`.
      Parameters:
        `is` - the `InputStream`
      Throws:
        `IOException` - if an error occurs while reading the XML-document from the `InputStream`

    - #### XPathProcessor

          public XPathProcessor(Document document)
                         throws IOException

      Throws:
        `IOException`

  - ### Method Detail

    - #### setNamespace

          public void setNamespace(String prefix,
                                   String namespace)

      Sets the namespace and the prefix to use for that namespace.
      Example:

           XPathProcessor xpath = ...;
           xpath.setNamespace("appng", "http://www.appng.org/schema/platform");
           xpath.getNode("/appng:platform//appng:action[@id=\"foo\"]")

      Parameters:
        `prefix` - the prefix
        `namespace` - the namespace

    - #### getXml

          public String getXml(Node node)

      Returns the XML-fragment represented by the given `Node`.
      Parameters:
        `node` - the `Node`
      Returns:
        the XML-fragment

    - #### getXml

          public void getXml(Node node,
                             OutputStream outputStream)

      Writes XML-fragment represented by the given `Node` into the given `OutputStream`.
      Parameters:
        `node` - the `Node`
        `outputStream` - the `OutputStream`

    - #### getXml

          public String getXml()

      Returns the XML for the `Document` parsed from the `URL`/`InputStream`.
      Returns:
        the XML-`String`

    - #### getXml

          public void getXml(OutputStream outputStream)

      Writes the XML for the `Document` parsed from the `URL`/`InputStream` into the given `OutputStream`.
      Parameters:
        `outputStream` - the `OutputStream`.

    - #### getXml

          public String getXml(NodeList nodes)

      Returns the XML-fragment represented by the given `NodeList`.
      Parameters:
        `nodes` - the `NodeList`
      Returns:
        the XML-fragment

    - #### getString

          public String getString(String xpathExpression)

      Parses the given `xpathExpression` to retrieve a `String`.
      Parameters:
        `xpathExpression` - the xpath-expression
      Returns:
        the `String` retrieved from the `xpathExpression` (may be `null`), or `null` if the expression could not be parsed.

    - #### getString

          public String getString(Node node,
                                  String xpathExpression)

      Parses the given `xpathExpression` to retrieve a `String`.
      Parameters:
        `node` - the `Node` to apply the expression to
        `xpathExpression` - the xpath-expression
      Returns:
        the `String` retrieved from the `xpathExpression` (may be `null`), or `null` if the expression could not be parsed.

    - #### getBoolean

          public Boolean getBoolean(String xpathExpression)

      Parses the given `xpathExpression` to retrieve a `Boolean`.
      Parameters:
        `xpathExpression` - the xpath-expression
      Returns:
        the `Boolean` retrieved from the `xpathExpression` (may be `null`), or `null` if the expression could not be parsed.

    - #### getBoolean

          public Boolean getBoolean(Node node,
                                    String xpathExpression)

      Parses the given `xpathExpression` to retrieve a `Boolean`.
      Parameters:
        `node` - the `Node` to apply the expression to
        `xpathExpression` - the xpath-expression
      Returns:
        the `Boolean` retrieved from the `xpathExpression` (may be `null`), or `null` if the expression could not be parsed.

    - #### getNumber

          public Number getNumber(String xpathExpression)

      Parses the given `xpathExpression` to retrieve a `Number`.
      Parameters:
        `xpathExpression` - the xpath-expression
      Returns:
        the `Number` retrieved from the `xpathExpression` (may be `null`), or `null` if the expression could not be parsed.

    - #### getNumber

          public Number getNumber(Node node,
                                  String xpathExpression)

      Parses the given `xpathExpression` to retrieve a `Number`.
      Parameters:
        `node` - the `Node` to apply the expression to
        `xpathExpression` - the xpath-expression
      Returns:
        the `Number` retrieved from the `xpathExpression` (may be `null`), or `null` if the expression could not be parsed.

    - #### getNode

          public Node getNode(String xpathExpression)

      Parses the given `xpathExpression` to retrieve a `Node`.
      Parameters:
        `xpathExpression` - the xpath-expression
      Returns:
        the `Node` retrieved from the `xpathExpression` (may be `null`), or `null` if the expression could not be parsed.

    - #### getNode

          public Node getNode(Node node,
                              String xpathExpression)

      Parses the given `xpathExpression` to retrieve a `Node`.
      Parameters:
        `node` - the `Node` to apply the expression to
        `xpathExpression` - the xpath-expression
      Returns:
        the `Node` retrieved from the `xpathExpression` (may be `null`), or `null` if the expression could not be parsed.

    - #### getElement

          public Element getElement(String xpathExpression)

      Parses the given `xpathExpression` to retrieve an `Element`.
      Parameters:
        `xpathExpression` - the xpath-expression
      Returns:
        the `Element` retrieved from the `xpathExpression` (may be `null`), or `null` if the expression could not be parsed.

    - #### getElement

          public Element getElement(Node node,
                                    String xpathExpression)

      Parses the given `xpathExpression` to retrieve an `Element`.
      Parameters:
        `node` - the `Node` to apply the expression to
        `xpathExpression` - the xpath-expression
      Returns:
        the `Element` retrieved from the `xpathExpression` (may be `null`), or `null` if the expression could not be parsed.

    - #### getNodes

          public NodeList getNodes(String xpathExpression)

      Parses the given `xpathExpression` to retrieve a `NodeList`.
      Parameters:
        `xpathExpression` - the xpath-expression
      Returns:
        the `NodeList` retrieved from the `xpathExpression` (may be `null`), or `null` if the expression could not be parsed.

    - #### getNodes

          public NodeList getNodes(Node node,
                                   String xpathExpression)

      Parses the given `xpathExpression` to retrieve a `NodeList`.
      Parameters:
        `node` - the `Node` to apply the expression to
        `xpathExpression` - the xpath-expression
      Returns:
        the `NodeList` retrieved from the `xpathExpression` (may be `null`), or `null` if the expression could not be parsed.

    - #### newAttribute

          public Attr newAttribute(String name,
                                   String value)

      Creates a new `Attr`ibute.
      Parameters:
        `name` - the name of the attribute
        `value` - the value of the attribute
      Returns:
        the new `Attr`ibute.

    - #### newElement

          public Element newElement(String tagName)

      Creates a new `Element`.
      Parameters:
        `tagName` - the tag-name for the element
      Returns:
        the new `Element`

    - #### newCDATA

          public CDATASection newCDATA(String tagName)

      Creates a new `CDATASection`.
      Parameters:
        `tagName` - the tag-name for the CDATA-section
      Returns:
        the new `CDATASection`

    - #### newText

          public Text newText(String tagName)

      Creates a new `Text`.
      Parameters:
        `tagName` - the tag-name for the text
      Returns:
        the new `Text`

    - #### addAttribute

          public Node addAttribute(Node node,
                                   String name,
                                   String value)

      Adds a new `Attr`ibute to the given `Node`.
      Parameters:
        `node` - the Node to add the `Attr`ibute to
        `name` - the name of the `Attr`ibute to add
        `value` - the value of the `Attr`ibute to add
      Returns:
        the `node` with the added `Attr`ibute
      See Also:
        `newAttribute(String, String)`

    - #### getDocument

          public Document getDocument()

      Returns the `Document` parsed from the `URL`/`InputStream`.
      Returns:
        the `Document`

Copyright © 2011–2023 aiticon GmbH. All rights reserved.