ca.infoway.messagebuilder.j5goodies
Class XPathHelper

Package class diagram package XPathHelper
java.lang.Object
  extended by ca.infoway.messagebuilder.j5goodies.XPathHelper

public class XPathHelper
extends Object

A utility to make XPath operations easier.

This utility supports both namespace-based and non-namespaced based operations. If the provided node was created with a namespace-aware parser, then you must use the namespace version of these methods.

For example:

 NodeList list = new XPathHelper().getNodes(document, 
                     "//x:myElement", "urn:mySchema.example.org");
 

In this example, the "x" prefix acts as a simple marker for the namespace; it doesn't matter what the name of the prefix is.

Author:
BC Holmes

Constructor Summary
XPathHelper()
           
 
Method Summary
 String getAttributeValue(Node base, String xpathExpression, String namespace)
          Get an attribute value.
 NodeList getNodes(Node base, String xpathExpression)
          Get a list of nodes.
 NodeList getNodes(Node base, String xpathExpression, String namespace)
          Get a list of nodes.
 Node getSingleNode(Node base, String xpathExpression)
          Get a single node.
 Node getSingleNode(Node base, String xpathExpression, String namespace)
          Get a single node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XPathHelper

public XPathHelper()
Method Detail

getSingleNode

public Node getSingleNode(Node base,
                          String xpathExpression)
                   throws XPathExpressionException

Get a single node.

Parameters:
base - - the base node
xpathExpression - - the xpath expression.
Returns:
- the node.
Throws:
XPathExpressionException - - if the XPath is not valid

getNodes

public NodeList getNodes(Node base,
                         String xpathExpression)
                  throws XPathExpressionException

Get a list of nodes.

Parameters:
base - - the base node
xpathExpression - - the xpath expression.
Returns:
- the node list.
Throws:
XPathExpressionException - - if the XPath is not valid

getNodes

public NodeList getNodes(Node base,
                         String xpathExpression,
                         String namespace)
                  throws XPathExpressionException

Get a list of nodes.

Parameters:
base - - the base node
xpathExpression - - the xpath expression (namespace aware).
namespace - - the namespace.
Returns:
- the node list.
Throws:
XPathExpressionException - - if the XPath is not valid

getSingleNode

public Node getSingleNode(Node base,
                          String xpathExpression,
                          String namespace)
                   throws XPathExpressionException

Get a single node.

Parameters:
base - - the base node
xpathExpression - - the xpath expression (namespace aware).
namespace - - the namespace.
Returns:
- the node.
Throws:
XPathExpressionException - - if the XPath is not valid

getAttributeValue

public String getAttributeValue(Node base,
                                String xpathExpression,
                                String namespace)
                         throws XPathExpressionException

Get an attribute value.

Parameters:
base - - the base node
xpathExpression - - the xpath expression (namespace aware).
namespace - - the namespace.
Returns:
- the attribute value.
Throws:
XPathExpressionException - - if the XPath is not valid


Copyright © 2013. All Rights Reserved.