com.jayway.restassured.path.xml
Class XmlPath

java.lang.Object
  extended by com.jayway.restassured.path.xml.XmlPath

public class XmlPath
extends Object

XmlPath is an alternative to using XPath for easily getting values from an XML document. It follows the Groovy syntax described here.
Let's say we have an XML defined as;

 <shopping>
       <category type="groceries">
           <item>
              <name>Chocolate</name>
              <price>10</price>
           </item>
           <item>
              <name>Coffee</name>
              <price>20</price>
           </item>
       </category>
       <category type="supplies">
           <item>
               <name>Paper</name>
               <price>5</price>
           </item>
           <item quantity="4">
               <name>Pens</name>
               <price>15</price>
           </item>
       </category>
       <category type="present">
           <item when="Aug 10">
               <name>Kathryn's Birthday</name>
               <price>200</price>
           </item>
     </category>
 </shopping>
 
Get the name of the first category item:
     String name = with(XML).get("shopping.category.item[0].name");
 
To get the number of category items:
     int items = with(XML).get("shopping.category.item.size()");
 
Get a specific category:
     Node category = with(XML).get("shopping.category[0]");
 
To get the number of categories with type attribute equal to 'groceries':
    int items = with(XML).get("shopping.category.findAll { it.@type == 'groceries' }.size()");
 
Get all items with price greater than or equal to 10 and less than or equal to 20:
 List<Node> itemsBetweenTenAndTwenty = with(XML).get("shopping.category.item.findAll { item -> def price = item.price.toFloat(); price >= 10 && price <= 20 }");
 


Constructor Summary
XmlPath(File file)
          Instantiate a new XmlPath instance.
XmlPath(InputSource source)
          Instantiate a new XmlPath instance.
XmlPath(InputStream stream)
          Instantiate a new XmlPath instance.
XmlPath(Reader reader)
          Instantiate a new XmlPath instance.
XmlPath(String text)
          Instantiate a new XmlPath instance.
XmlPath(URI uri)
          Instantiate a new XmlPath instance.
 
Method Summary
<T> T
get(String path)
          Get the result of an XML path expression.
 boolean getBoolean(String path)
          Get the result of an XML path expression as a boolean.
 byte getByte(String path)
          Get the result of an XML path expression as a byte.
 char getChar(String path)
          Get the result of an XML path expression as a char.
 double getDouble(String path)
          Get the result of an XML path expression as a double.
 float getFloat(String path)
          Get the result of an XML path expression as a float.
 int getInt(String path)
          Get the result of an XML path expression as an int.
<T> List<T>
getList(String path)
          Get the result of an XML path expression as a list.
<T> List<T>
getList(String path, Class<T> genericType)
          Get the result of an XML path expression as a list.
 long getLong(String path)
          Get the result of an XML path expression as a long.
<K,V> Map<K,V>
getMap(String path)
          Get the result of an XML path expression as a map.
<K,V> Map<K,V>
getMap(String path, Class<K> keyType, Class<V> valueType)
          Get the result of an XML path expression as a map.
 short getShort(String path)
          Get the result of an XML path expression as a short.
 String getString(String path)
          Get the result of an XML path expression as a string.
static XmlPath given(File file)
          Instantiate a new XmlPath instance.
static XmlPath given(InputSource source)
          Instantiate a new XmlPath instance.
static XmlPath given(InputStream stream)
          Instantiate a new XmlPath instance.
static XmlPath given(Reader reader)
          Instantiate a new XmlPath instance.
static XmlPath given(String text)
          Instantiate a new XmlPath instance.
static XmlPath given(URI uri)
          Instantiate a new XmlPath instance.
 XmlPath setRoot(String rootPath)
          Set the root path of the document so that you don't need to write the entire path.
static XmlPath with(File file)
          Instantiate a new XmlPath instance.
static XmlPath with(InputSource source)
          Instantiate a new XmlPath instance.
static XmlPath with(InputStream stream)
           
static XmlPath with(Reader reader)
          Instantiate a new XmlPath instance.
static XmlPath with(String text)
          Instantiate a new XmlPath instance.
static XmlPath with(URI uri)
          Instantiate a new XmlPath instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlPath

public XmlPath(String text)
Instantiate a new XmlPath instance.

Parameters:
text - The text containing the XML document

XmlPath

public XmlPath(InputStream stream)
Instantiate a new XmlPath instance.

Parameters:
stream - The stream containing the XML document

XmlPath

public XmlPath(InputSource source)
Instantiate a new XmlPath instance.

Parameters:
source - The source containing the XML document

XmlPath

public XmlPath(File file)
Instantiate a new XmlPath instance.

Parameters:
file - The file containing the XML document

XmlPath

public XmlPath(Reader reader)
Instantiate a new XmlPath instance.

Parameters:
reader - The reader containing the XML document

XmlPath

public XmlPath(URI uri)
Instantiate a new XmlPath instance.

Parameters:
uri - The URI containing the XML document
Method Detail

get

public <T> T get(String path)
Get the result of an XML path expression. For syntax details please refer to this url.

Type Parameters:
T - The type of the return value.
Parameters:
path - The XML path.
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getList

public <T> List<T> getList(String path)
Get the result of an XML path expression as a list. For syntax details please refer to this url.

Type Parameters:
T - The list type
Parameters:
path - The XML path.
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getList

public <T> List<T> getList(String path,
                           Class<T> genericType)
Get the result of an XML path expression as a list. For syntax details please refer to this url.

Type Parameters:
T - The type
Parameters:
path - The XML path.
genericType - The generic list type
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getMap

public <K,V> Map<K,V> getMap(String path)
Get the result of an XML path expression as a map. For syntax details please refer to this url.

Type Parameters:
K - The type of the expected key
V - The type of the expected value
Parameters:
path - The XML path.
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getMap

public <K,V> Map<K,V> getMap(String path,
                             Class<K> keyType,
                             Class<V> valueType)
Get the result of an XML path expression as a map. For syntax details please refer to this url.

Type Parameters:
K - The type of the expected key
V - The type of the expected value
Parameters:
path - The XML path.
keyType - The type of the expected key
valueType - The type of the expected value
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getInt

public int getInt(String path)
Get the result of an XML path expression as an int. For syntax details please refer to this url.

Parameters:
path - The XML path.
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getBoolean

public boolean getBoolean(String path)
Get the result of an XML path expression as a boolean. For syntax details please refer to this url.

Parameters:
path - The XML path.
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getChar

public char getChar(String path)
Get the result of an XML path expression as a char. For syntax details please refer to this url.

Parameters:
path - The XML path.
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getByte

public byte getByte(String path)
Get the result of an XML path expression as a byte. For syntax details please refer to this url.

Parameters:
path - The XML path.
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getShort

public short getShort(String path)
Get the result of an XML path expression as a short. For syntax details please refer to this url.

Parameters:
path - The XML path.
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getFloat

public float getFloat(String path)
Get the result of an XML path expression as a float. For syntax details please refer to this url.

Parameters:
path - The XML path.
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getDouble

public double getDouble(String path)
Get the result of an XML path expression as a double. For syntax details please refer to this url.

Parameters:
path - The XML path.
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getLong

public long getLong(String path)
Get the result of an XML path expression as a long. For syntax details please refer to this url.

Parameters:
path - The XML path.
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

getString

public String getString(String path)
Get the result of an XML path expression as a string. For syntax details please refer to this url.

Parameters:
path - The XML path.
Returns:
The object matching the XML path. A will be thrown if the object cannot be casted to the expected type.

given

public static XmlPath given(String text)
Instantiate a new XmlPath instance.

Parameters:
text - The text containing the XML document

given

public static XmlPath given(InputStream stream)
Instantiate a new XmlPath instance.

Parameters:
stream - The stream containing the XML document

given

public static XmlPath given(InputSource source)
Instantiate a new XmlPath instance.

Parameters:
source - The source containing the XML document

given

public static XmlPath given(File file)
Instantiate a new XmlPath instance.

Parameters:
file - The file containing the XML document

given

public static XmlPath given(Reader reader)
Instantiate a new XmlPath instance.

Parameters:
reader - The reader containing the XML document

given

public static XmlPath given(URI uri)
Instantiate a new XmlPath instance.

Parameters:
uri - The URI containing the XML document

with

public static XmlPath with(InputStream stream)

with

public static XmlPath with(String text)
Instantiate a new XmlPath instance.

Parameters:
text - The text containing the XML document

with

public static XmlPath with(InputSource source)
Instantiate a new XmlPath instance.

Parameters:
source - The source containing the XML document

with

public static XmlPath with(File file)
Instantiate a new XmlPath instance.

Parameters:
file - The file containing the XML document

with

public static XmlPath with(Reader reader)
Instantiate a new XmlPath instance.

Parameters:
reader - The reader containing the XML document

with

public static XmlPath with(URI uri)
Instantiate a new XmlPath instance.

Parameters:
uri - The URI containing the XML document

setRoot

public XmlPath setRoot(String rootPath)
Set the root path of the document so that you don't need to write the entire path. E.g.
 final XmlPath xmlPath = new XmlPath(XML).setRoot("shopping.category.item");
 assertThat(xmlPath.getInt("size()"), equalTo(5));
 assertThat(xmlPath.getList("children().list()", String.class), hasItem("Pens"));
 

Parameters:
rootPath - The root path to use.


Copyright © 2010-2011. All Rights Reserved.