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>);
Modifier and Type | Class and Description |
---|---|
static class |
XmlPath.CompatibilityMode |
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 . |
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.
|
String |
prettify()
Get the XML as a prettified string.
|
String |
prettyPrint()
Get and print the XML as a prettified string.
|
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.
|
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 Node get()
public <T> T get(String path)
T
- The type of the return value.path
- The XML path.public <T> List<T> getList(String path)
T
- The list typepath
- The XML path.public <T> List<T> getList(String path, Class<T> genericType)
T
- The typepath
- The XML path.genericType
- The generic list typepublic <K,V> Map<K,V> getMap(String path)
K
- The type of the expected keyV
- The type of the expected valuepath
- The XML path.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 valuepublic int getInt(String path)
path
- The XML path.public boolean getBoolean(String path)
path
- The XML path.public Node getNode(String path)
path
- The XML path.public NodeChildren getNodeChildren(String path)
NodeChildren
. For syntax details please refer to
this url.path
- The XML path.public char getChar(String path)
path
- The XML path.public byte getByte(String path)
path
- The XML path.public short getShort(String path)
path
- The XML path.public float getFloat(String path)
path
- The XML path.public double getDouble(String path)
path
- The XML path.public long getLong(String path)
path
- The XML path.public String getString(String path)
path
- The XML path.public String prettify()
public String prettyPrint()
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 documentpublic XmlPath setRoot(String rootPath)
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"));
rootPath
- The root path to use.Copyright © 2010-2012. All Rights Reserved.