public class XmlPath extends Object
<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 }");Get the chocolate price:
int priceOfChocolate = with(XML).getInt("**.find { it.name == 'Chocolate' }.price"You can also parse HTML by setting compatibility mode to HTML:
XmlPath xmlPath = new XmlPath(CompatibilityMode.HTML,<some html>);The XmlPath implementation of rest-assured uses a Groovy shell to evaluate expressions so be careful when injecting user input into the expression. For example avoid doing this:
String type = System.console().readLine(); List<Map> books = with(Object).get("shopping.category.findAll { it.@type == '"+type+"' }");Instead use the
param(java.lang.String, java.lang.Object)
method like this:
String type = System.console().readLine(); List<Map> books = with(Object).param("type", type).get("shopping.category.findAll { it.@type == type}");
Modifier and Type | Class and Description |
---|---|
static class |
XmlPath.CompatibilityMode |
Modifier and Type | Field and Description |
---|---|
static XmlPathConfig |
config |
Constructor and Description |
---|
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.
|
XmlPath(XmlPath.CompatibilityMode mode,
File file)
Instantiate a new XmlPath instance.
|
XmlPath(XmlPath.CompatibilityMode mode,
InputSource source)
Instantiate a new XmlPath instance.
|
XmlPath(XmlPath.CompatibilityMode mode,
InputStream stream)
Instantiate a new XmlPath instance.
|
XmlPath(XmlPath.CompatibilityMode mode,
Reader reader)
Instantiate a new XmlPath instance.
|
XmlPath(XmlPath.CompatibilityMode mode,
String text)
Instantiate a new XmlPath instance.
|
XmlPath(XmlPath.CompatibilityMode mode,
URI uri)
Instantiate a new XmlPath instance.
|
Modifier and Type | Method and Description |
---|---|
static XmlPath |
from(File file)
Instantiate a new XmlPath instance.
|
static XmlPath |
from(InputSource source)
Instantiate a new XmlPath instance.
|
static XmlPath |
from(InputStream stream)
Instantiate a new XmlPath instance.
|
static XmlPath |
from(Reader reader)
Instantiate a new XmlPath instance.
|
static XmlPath |
from(String text)
Instantiate a new XmlPath instance.
|
static XmlPath |
from(URI uri)
Instantiate a new XmlPath instance.
|
Node |
get()
Get the entire XML graph as an Object
this url.
|
<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.
|
Node |
getNode(String path)
Get the result of an XML path expression as a
Node . |
NodeChildren |
getNodeChildren(String path)
Get the result of an XML path expression as a
NodeChildren . |
<T> T |
getObject(String path,
Class<T> objectType)
Get an XML document as a Java Object.
|
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.
|
UUID |
getUUID(String path)
Get the result of an XML path expression as a UUID.
|
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 |
param(String key,
Object value)
Add a parameter for the expression.
|
XmlPath |
peek()
Peeks into the XML/HTML that XmlPath will parse by printing it to the console.
|
String |
prettify()
Get the XML as a prettified string.
|
XmlPath |
prettyPeek()
Peeks into the XML/HTML that XmlPath will parse by printing it to the console in a prettified manner.
|
String |
prettyPrint()
Get and print the XML as a prettified string.
|
static void |
reset()
Resets static XmlPath configuration to default values
|
XmlPath |
setRoot(String rootPath)
Deprecated.
Use
setRootPath(String) instead |
XmlPath |
setRootPath(String rootPath)
Set the root path of the document so that you don't need to write the entire path.
|
XmlPath |
using(JAXBObjectMapperFactory factory)
Configure XmlPath to use a specific JAXB object mapper factory
|
XmlPath |
using(XmlPathConfig config)
Configure XmlPath to with a specific XmlPathConfig.
|
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.
|
public static XmlPathConfig config
public XmlPath(String text)
text
- The text containing the XML documentpublic XmlPath(InputStream stream)
stream
- The stream containing the XML documentpublic XmlPath(InputSource source)
source
- The source containing the XML documentpublic XmlPath(File file)
file
- The file containing the XML documentpublic XmlPath(Reader reader)
reader
- The reader containing the XML documentpublic XmlPath(URI uri)
uri
- The URI containing the XML documentpublic XmlPath(XmlPath.CompatibilityMode mode, String text)
mode
- The compatibility modetext
- The text containing the XML documentpublic XmlPath(XmlPath.CompatibilityMode mode, InputStream stream)
mode
- The compatibility modestream
- The stream containing the XML documentpublic XmlPath(XmlPath.CompatibilityMode mode, InputSource source)
mode
- The compatibility modesource
- The source containing the XML documentpublic XmlPath(XmlPath.CompatibilityMode mode, File file)
mode
- The compatibility modefile
- The file containing the XML documentpublic XmlPath(XmlPath.CompatibilityMode mode, Reader reader)
mode
- The compatibility modereader
- The reader containing the XML documentpublic XmlPath(XmlPath.CompatibilityMode mode, URI uri)
mode
- The compatibility modeuri
- The URI containing the XML documentpublic XmlPath using(JAXBObjectMapperFactory factory)
factory
- The JAXB object mapper factory instancepublic XmlPath using(XmlPathConfig config)
config
- The XmlPath configpublic Node get()
ClassCastException
will be thrown if the object
cannot be casted to the expected type.public <T> T get(String path)
T
- The type of the return value.path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public <T> List<T> getList(String path)
T
- The list typepath
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public <T> List<T> getList(String path, Class<T> genericType)
T
- The typepath
- The XML path.genericType
- The generic list typeClassCastException
will be thrown if the object
cannot be casted to the expected type.public <K,V> Map<K,V> getMap(String path)
K
- The type of the expected keyV
- The type of the expected valuepath
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public <K,V> Map<K,V> getMap(String path, Class<K> keyType, Class<V> valueType)
K
- The type of the expected keyV
- The type of the expected valuepath
- The XML path.keyType
- The type of the expected keyvalueType
- The type of the expected valueClassCastException
will be thrown if the object
cannot be casted to the expected type.public <T> T getObject(String path, Class<T> objectType)
T
- The type of the java objectobjectType
- The type of the java object.public int getInt(String path)
path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public boolean getBoolean(String path)
path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public Node getNode(String path)
path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public NodeChildren getNodeChildren(String path)
NodeChildren
. For syntax details please refer to
this url.path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public char getChar(String path)
path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public byte getByte(String path)
path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public short getShort(String path)
path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public float getFloat(String path)
path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public double getDouble(String path)
path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public long getLong(String path)
path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public String getString(String path)
path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public UUID getUUID(String path)
path
- The XML path.ClassCastException
will be thrown if the object
cannot be casted to the expected type.public XmlPath param(String key, Object value)
String type = System.console().readLine(); List<Map> books = with(Object).param("type", type).get("shopping.category.findAll { it.@type == type}");
key
- The name of the parameter. Just use this name in your expression as a variablevalue
- The value of the parameterpublic XmlPath peek()
prettify()
. If you want to return a prettified version of the content and also print it to the console use prettyPrint()
.
Note that the content is not guaranteed to be looking exactly like the it does at the source. This is because once you peek the content has been downloaded and transformed into another data structure (used by XmlPath) and the XML is rendered from this data structure.
public XmlPath prettyPeek()
prettify()
. If you want to return a prettified version of the content and also print it to the console use prettyPrint()
.
Note that the content is not guaranteed to be looking exactly like the it does at the source. This is because once you peek the content has been downloaded and transformed into another data structure (used by XmlPath) and the XML is rendered from this data structure.
public String prettify()
Note that the content is not guaranteed to be looking exactly like the it does at the source. This is because once you peek the content has been downloaded and transformed into another data structure (used by XmlPath) and the XML is rendered from this data structure.
public String prettyPrint()
Note that the content is not guaranteed to be looking exactly like the it does at the source. This is because once you peek the content has been downloaded and transformed into another data structure (used by XmlPath) and the XML is rendered from this data structure.
public static XmlPath given(String text)
text
- The text containing the XML documentpublic static XmlPath given(InputStream stream)
stream
- The stream containing the XML documentpublic static XmlPath given(InputSource source)
source
- The source containing the XML documentpublic static XmlPath given(File file)
file
- The file containing the XML documentpublic static XmlPath given(Reader reader)
reader
- The reader containing the XML documentpublic static XmlPath given(URI uri)
uri
- The URI containing the XML documentpublic static XmlPath with(InputStream stream)
public static XmlPath with(String text)
text
- The text containing the XML documentpublic static XmlPath with(InputSource source)
source
- The source containing the XML documentpublic static XmlPath with(File file)
file
- The file containing the XML documentpublic static XmlPath with(Reader reader)
reader
- The reader containing the XML documentpublic static XmlPath with(URI uri)
uri
- The URI containing the XML documentpublic static XmlPath from(InputStream stream)
stream
- The stream containing the XML documentpublic static XmlPath from(String text)
text
- The text containing the XML documentpublic static XmlPath from(InputSource source)
source
- The source containing the XML documentpublic static XmlPath from(File file)
file
- The file containing the XML documentpublic static XmlPath from(Reader reader)
reader
- The reader containing the XML documentpublic static XmlPath from(URI uri)
uri
- The URI containing the XML document@Deprecated public XmlPath setRoot(String rootPath)
setRootPath(String)
insteadfinal XmlPath xmlPath = new XmlPath(XML).setRoot("shopping.category.item"); assertThat(xmlPath.getInt("size()"), equalTo(5)); assertThat(xmlPath.getList("children().list()", String.class), hasItem("Pens"));
rootPath
- The root path to use.public XmlPath setRootPath(String rootPath)
final XmlPath xmlPath = new XmlPath(XML).setRootPath("shopping.category.item"); assertThat(xmlPath.getInt("size()"), equalTo(5)); assertThat(xmlPath.getList("children().list()", String.class), hasItem("Pens"));
rootPath
- The root path to use.public static void reset()
Copyright © 2010–2022. All rights reserved.