org.javalite.test
Class XPathHelper

java.lang.Object
  extended by org.javalite.test.XPathHelper

public class XPathHelper
extends Object

Convenience class for pulling information from XML documents. It provides a set of convenience methods for checking structure and content of XML files. Such files could be XHTML generated for web apps, or any other XML.

Author:
Igor Polevoy

Constructor Summary
XPathHelper(String xml)
          Use constructor and instance methods to only parse once and reuse a parsed tree.
 
Method Summary
 String attributeValue(String xpath)
          Returns a value of an attribute.
static String attributeValue(String xpath, String xml)
          Selects a value of attribute.
 int count(String xpath)
          Counts a collection selected by XPath expression.
static int count(String xpath, String xml)
          Counts a collection selected by XPath expression.
static List<String> selectStrings(String xpath, String xml)
          Selects text nodes as list of strings
 String selectText(String xpath)
          Retrieves text of a single node.
static String selectText(String xpath, String xml)
          Selects text from 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(String xml)
Use constructor and instance methods to only parse once and reuse a parsed tree. Use this method in high performance applicaitons when you need to pull many values from the same document.

Parameters:
xml - XML to parse.
Method Detail

selectText

public String selectText(String xpath)
Retrieves text of a single node.

Parameters:
xpath - XPath pointing to a single node (not its text).
Returns:
text of a node.

attributeValue

public String attributeValue(String xpath)
Returns a value of an attribute.

Parameters:
xpath - needs to point to an attribute of a single node.
Returns:
value of an attribute of a single node.

count

public int count(String xpath)
Counts a collection selected by XPath expression.

Parameters:
xpath - expression which muse evaluate to a list of items.
Returns:
size of a collection selected by expression.

selectText

public static String selectText(String xpath,
                                String xml)
Selects text from a single node.

Parameters:
xpath - expression that points to a single node.
xml - document.
Returns:
text from a selected node.

count

public static int count(String xpath,
                        String xml)
Counts a collection selected by XPath expression.

Parameters:
xpath - expression which mus evaluate to a list of items.
xml - xml document.
Returns:
size of a collection selected by expression.

attributeValue

public static String attributeValue(String xpath,
                                    String xml)
Selects a value of attribute.

Parameters:
xpath - expression that points to a specific attribute of a specific node. Example: /a/b[1]/@id.
xml - document.
Returns:
value of selected attribute.

selectStrings

public static List<String> selectStrings(String xpath,
                                         String xml)
Selects text nodes as list of strings

Parameters:
xpath - xpath expression, should end with text() function, example: "//name/text()"
xml - xml to get strings from.
Returns:
list of found strings matching expression, never null. If nothing matches, list will be empty.


Copyright © 2015 JavaLite. All rights reserved.