Class XpathUtils
- java.lang.Object
-
- com.amazonaws.util.XpathUtils
-
public class XpathUtils extends Object
Utility methods for extracting data from XML documents using Xpath expressions.
-
-
Constructor Summary
Constructors Constructor Description XpathUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Boolean
asBoolean(String expression, Node node)
Evaluates the specified XPath expression and returns the result as a Boolean.static Boolean
asBoolean(String expression, Node node, XPath xpath)
Same asasBoolean(String, Node)
but allows an xpath to be passed in explicitly for reuse.static Byte
asByte(String expression, Node node)
Evaluates the specified XPath expression and returns the result as a Byte.static Byte
asByte(String expression, Node node, XPath xpath)
Same asasByte(String, Node)
but allows an xpath to be passed in explicitly for reuse.static ByteBuffer
asByteBuffer(String expression, Node node)
Evaluates the specified xpath expression, base64 decodes the data and returns the result as a ByteBuffer.static ByteBuffer
asByteBuffer(String expression, Node node, XPath xpath)
Same asasByteBuffer(String, Node)
but allows an xpath to be passed in explicitly for reuse.static Date
asDate(String expression, Node node)
Evaluates the specified XPath expression and returns the result as a Date.static Date
asDate(String expression, Node node, XPath xpath)
Same asasDate(String, Node)
but allows an xpath to be passed in explicitly for reuse.static Double
asDouble(String expression, Node node)
Evaluates the specified XPath expression and returns the results as a Double.static Double
asDouble(String expression, Node node, XPath xpath)
Same asasDouble(String, Node)
but allows an xpath to be passed in explicitly for reuse.static Float
asFloat(String expression, Node node)
Evaluates the specified XPath expression and returns the result as a Float.static Float
asFloat(String expression, Node node, XPath xpath)
Same asasFloat(String, Node)
but allows an xpath to be passed in explicitly for reuse.static Integer
asInteger(String expression, Node node)
Evaluates the specified XPath expression and returns the result as an Integer.static Integer
asInteger(String expression, Node node, XPath xpath)
Same asasInteger(String, Node)
but allows an xpath to be passed in explicitly for reuse.static Long
asLong(String expression, Node node)
Evaluates the specified XPath expression and returns the result as a Long.static Long
asLong(String expression, Node node, XPath xpath)
Same asasLong(String, Node)
but allows an xpath to be passed in explicitly for reuse.static Node
asNode(String nodeName, Node node)
Evaluates the specified XPath expression and returns the result as a Node.static Node
asNode(String nodeName, Node node, XPath xpath)
Same asasNode(String, Node)
but allows an xpath to be passed in explicitly for reuse.static String
asString(String expression, Node node)
Evaluates the specified XPath expression and returns the result as a string.static String
asString(String expression, Node node, XPath xpath)
Same asasString(String, Node)
but allows an xpath to be passed in explicitly for reuse.static Document
documentFrom(InputStream is)
This method closes the given input stream upon completion.static Document
documentFrom(String xml)
static Document
documentFrom(URL url)
static boolean
isEmpty(Node node)
Returns true if the specified node is null or has no children.static int
nodeLength(NodeList list)
Returns the length of the specified node list.static XPath
xpath()
Returns a new instance of XPath, which is not thread safe and not reentrant.
-
-
-
Method Detail
-
xpath
public static XPath xpath()
Returns a new instance of XPath, which is not thread safe and not reentrant.
-
documentFrom
public static Document documentFrom(InputStream is) throws SAXException, IOException, ParserConfigurationException
This method closes the given input stream upon completion.
-
documentFrom
public static Document documentFrom(String xml) throws SAXException, IOException, ParserConfigurationException
-
documentFrom
public static Document documentFrom(URL url) throws SAXException, IOException, ParserConfigurationException
-
asDouble
public static Double asDouble(String expression, Node node) throws XPathExpressionException
Evaluates the specified XPath expression and returns the results as a Double.This method can be expensive as a new xpath is instantiated per invocation. Consider passing in the xpath explicitly via {
asDouble(String, Node, XPath)
instead. NoteXPath
is not thread-safe and not reentrant.- Parameters:
expression
- The XPath expression to evaluate.node
- The node to run the expression on.- Returns:
- The Double result.
- Throws:
XPathExpressionException
- If there was a problem processing the specified XPath expression.
-
asDouble
public static Double asDouble(String expression, Node node, XPath xpath) throws XPathExpressionException
Same asasDouble(String, Node)
but allows an xpath to be passed in explicitly for reuse.- Throws:
XPathExpressionException
-
asString
public static String asString(String expression, Node node) throws XPathExpressionException
Evaluates the specified XPath expression and returns the result as a string.This method can be expensive as a new xpath is instantiated per invocation. Consider passing in the xpath explicitly via {
asDouble(String, Node, XPath)
instead. NoteXPath
is not thread-safe and not reentrant.- Parameters:
expression
- The XPath expression to evaluate.node
- The node to run the expression on.- Returns:
- The string result.
- Throws:
XPathExpressionException
- If there was a problem processing the specified XPath expression.
-
asString
public static String asString(String expression, Node node, XPath xpath) throws XPathExpressionException
Same asasString(String, Node)
but allows an xpath to be passed in explicitly for reuse.- Throws:
XPathExpressionException
-
asInteger
public static Integer asInteger(String expression, Node node) throws XPathExpressionException
Evaluates the specified XPath expression and returns the result as an Integer.This method can be expensive as a new xpath is instantiated per invocation. Consider passing in the xpath explicitly via {
asDouble(String, Node, XPath)
instead. NoteXPath
is not thread-safe and not reentrant.- Parameters:
expression
- The XPath expression to evaluate.node
- The node to run the expression on.- Returns:
- The Integer result.
- Throws:
XPathExpressionException
- If there was a problem processing the specified XPath expression.
-
asInteger
public static Integer asInteger(String expression, Node node, XPath xpath) throws XPathExpressionException
Same asasInteger(String, Node)
but allows an xpath to be passed in explicitly for reuse.- Throws:
XPathExpressionException
-
asBoolean
public static Boolean asBoolean(String expression, Node node) throws XPathExpressionException
Evaluates the specified XPath expression and returns the result as a Boolean.This method can be expensive as a new xpath is instantiated per invocation. Consider passing in the xpath explicitly via {
asDouble(String, Node, XPath)
instead. NoteXPath
is not thread-safe and not reentrant.- Parameters:
expression
- The XPath expression to evaluate.node
- The node to run the expression on.- Returns:
- The Boolean result.
- Throws:
XPathExpressionException
- If there was a problem processing the specified XPath expression.
-
asBoolean
public static Boolean asBoolean(String expression, Node node, XPath xpath) throws XPathExpressionException
Same asasBoolean(String, Node)
but allows an xpath to be passed in explicitly for reuse.- Throws:
XPathExpressionException
-
asFloat
public static Float asFloat(String expression, Node node) throws XPathExpressionException
Evaluates the specified XPath expression and returns the result as a Float.This method can be expensive as a new xpath is instantiated per invocation. Consider passing in the xpath explicitly via {
asDouble(String, Node, XPath)
instead. NoteXPath
is not thread-safe and not reentrant.- Parameters:
expression
- The XPath expression to evaluate.node
- The node to run the expression on.- Returns:
- The Float result.
- Throws:
XPathExpressionException
- If there was a problem processing the specified XPath expression.
-
asFloat
public static Float asFloat(String expression, Node node, XPath xpath) throws XPathExpressionException
Same asasFloat(String, Node)
but allows an xpath to be passed in explicitly for reuse.- Throws:
XPathExpressionException
-
asLong
public static Long asLong(String expression, Node node) throws XPathExpressionException
Evaluates the specified XPath expression and returns the result as a Long.This method can be expensive as a new xpath is instantiated per invocation. Consider passing in the xpath explicitly via {
asDouble(String, Node, XPath)
instead. NoteXPath
is not thread-safe and not reentrant.- Parameters:
expression
- The XPath expression to evaluate.node
- The node to run the expression on.- Returns:
- The Long result.
- Throws:
XPathExpressionException
- If there was a problem processing the specified XPath expression.
-
asLong
public static Long asLong(String expression, Node node, XPath xpath) throws XPathExpressionException
Same asasLong(String, Node)
but allows an xpath to be passed in explicitly for reuse.- Throws:
XPathExpressionException
-
asByte
public static Byte asByte(String expression, Node node) throws XPathExpressionException
Evaluates the specified XPath expression and returns the result as a Byte.This method can be expensive as a new xpath is instantiated per invocation. Consider passing in the xpath explicitly via {
asDouble(String, Node, XPath)
instead. NoteXPath
is not thread-safe and not reentrant.- Parameters:
expression
- The XPath expression to evaluate.node
- The node to run the expression on.- Returns:
- The Byte result.
- Throws:
XPathExpressionException
- If there was a problem processing the specified XPath expression.
-
asByte
public static Byte asByte(String expression, Node node, XPath xpath) throws XPathExpressionException
Same asasByte(String, Node)
but allows an xpath to be passed in explicitly for reuse.- Throws:
XPathExpressionException
-
asDate
public static Date asDate(String expression, Node node) throws XPathExpressionException
Evaluates the specified XPath expression and returns the result as a Date. Assumes that the node's text is formatted as an ISO 8601 date, as specified by xs:dateTime.This method can be expensive as a new xpath is instantiated per invocation. Consider passing in the xpath explicitly via {
asDouble(String, Node, XPath)
instead. NoteXPath
is not thread-safe and not reentrant.- Parameters:
expression
- The XPath expression to evaluate.node
- The node to run the expression on.- Returns:
- The Date result.
- Throws:
XPathExpressionException
- If there was a problem processing the specified XPath expression.
-
asDate
public static Date asDate(String expression, Node node, XPath xpath) throws XPathExpressionException
Same asasDate(String, Node)
but allows an xpath to be passed in explicitly for reuse.- Throws:
XPathExpressionException
-
asByteBuffer
public static ByteBuffer asByteBuffer(String expression, Node node) throws XPathExpressionException
Evaluates the specified xpath expression, base64 decodes the data and returns the result as a ByteBuffer.This method can be expensive as a new xpath is instantiated per invocation. Consider passing in the xpath explicitly via {
asDouble(String, Node, XPath)
instead. NoteXPath
is not thread-safe and not reentrant.- Parameters:
expression
- The Xpath expression to evaluate.node
- The node on which to evaluate the expression.- Returns:
- A ByteBuffer of base64 decoded data from the result of evaluating the specified Xpath expression.
- Throws:
XPathExpressionException
- If there are any problems evaluating the Xpath expression.
-
asByteBuffer
public static ByteBuffer asByteBuffer(String expression, Node node, XPath xpath) throws XPathExpressionException
Same asasByteBuffer(String, Node)
but allows an xpath to be passed in explicitly for reuse.- Throws:
XPathExpressionException
-
isEmpty
public static boolean isEmpty(Node node)
Returns true if the specified node is null or has no children.- Parameters:
node
- The node to test.- Returns:
- True if the specified node is null or has no children.
-
asNode
public static Node asNode(String nodeName, Node node) throws XPathExpressionException
Evaluates the specified XPath expression and returns the result as a Node.- Parameters:
nodeName
- The XPath expression to evaluate.node
- The node to run the expression on.- Returns:
- The Node result.
- Throws:
XPathExpressionException
- If there was a problem processing the specified XPath expression.
-
asNode
public static Node asNode(String nodeName, Node node, XPath xpath) throws XPathExpressionException
Same asasNode(String, Node)
but allows an xpath to be passed in explicitly for reuse.- Throws:
XPathExpressionException
-
nodeLength
public static int nodeLength(NodeList list)
Returns the length of the specified node list.- Parameters:
list
- The node list to measure.- Returns:
- The length of the specified node list.
-
-