Package net.jodah.xsylum
Class XmlElement
- java.lang.Object
-
- net.jodah.xsylum.XmlSearchable<Element>
-
- net.jodah.xsylum.XmlElement
-
public final class XmlElement extends XmlSearchable<Element>
A named XML element.
-
-
Field Summary
-
Fields inherited from class net.jodah.xsylum.XmlSearchable
source
-
-
Constructor Summary
Constructors Constructor Description XmlElement(Element element)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
attribute(String attribute)
Get the value of theattribute
.boolean
attributeAsBoolean(String attribute)
Get the value of theattribute
as a boolean.double
attributeAsDouble(String attribute)
Get the value of theattribute
as a double.<V extends Enum<V>>
VattributeAsEnum(String attribute, Class<V> targetEnum)
Get the value of theattribute
as an enum of typeV
.int
attributeAsInt(String attribute)
Get the value of theattribute
as an integer.long
attributeAsLong(String attribute)
Get the value of theattribute
as a long.Map<String,String>
attributes()
Builds and returns a map of the element's attributes, else empty map if the element has no attributes.List<XmlElement>
children()
Returns the element's children, else empty list if the element has no children.Element
element()
Returns the underlying element.XmlElement
get(int index)
Returns the XmlElement at theindex
, else null if none can be found or the node at theindex
is not an elementXmlElement
get(String tagName)
Returns the first child XmlElement matching thetagName
.List<XmlElement>
getAll(String tagName)
Returns all child XmlElements matching thetagName
.boolean
hasAttribute(String attribute)
Returns whether the element contains theattribute
.boolean
hasAttributes()
Returns whether the element contains attributes.boolean
hasChild(String name)
Returns whether the element contains any child elements with thename
.String
name()
Returns the element's name.String
toString()
String
toXml()
Returns a XML representation of the element, including its attributes and value.String
value()
Returns the text value of the element.boolean
valueAsBoolean()
Get the element value as a boolean.double
valueAsDouble()
Get the element value as a double.<V extends Enum<V>>
VvalueAsEnum(Class<V> targetEnum)
Get the element value as an enum of typeV
.int
valueAsInt()
Get the element value as an integer.long
valueAsLong()
Get the element value as a long.-
Methods inherited from class net.jodah.xsylum.XmlSearchable
find, findAll, value, valueAsBoolean, valueAsDouble, valueAsEnum, valueAsInt, valueAsLong, values, valuesAsBoolean, valuesAsDouble, valuesAsEnum, valuesAsInt, valuesAsLong
-
-
-
-
Constructor Detail
-
XmlElement
public XmlElement(Element element)
-
-
Method Detail
-
attribute
public String attribute(String attribute) throws XsylumException
Get the value of theattribute
.- Throws:
XsylumException
- if theattribute
cannot be found
-
attributeAsBoolean
public boolean attributeAsBoolean(String attribute) throws XsylumException
Get the value of theattribute
as a boolean. Returns true for "true", "1", "yes", "y" ignoring case, else returns false.- Throws:
XsylumException
- if theattribute
cannot be found
-
attributeAsDouble
public double attributeAsDouble(String attribute) throws XsylumException
Get the value of theattribute
as a double.- Throws:
XsylumException
- if theattribute
cannot be foundNumberFormatException
- if the attribute is not a valid double
-
attributeAsEnum
public <V extends Enum<V>> V attributeAsEnum(String attribute, Class<V> targetEnum) throws XsylumException
Get the value of theattribute
as an enum of typeV
. Returns null if value cannot be parsed to an enum of typeV
.- Type Parameters:
V
- enum type- Throws:
XsylumException
- if theattribute
cannot be found
-
attributeAsInt
public int attributeAsInt(String attribute) throws XsylumException
Get the value of theattribute
as an integer.- Throws:
XsylumException
- if theattribute
cannot be foundNumberFormatException
- if the attribute is not a valid int
-
attributeAsLong
public long attributeAsLong(String attribute) throws XsylumException
Get the value of theattribute
as a long.- Throws:
XsylumException
- if theattribute
cannot be foundNumberFormatException
- if the attribute is not a valid long
-
attributes
public Map<String,String> attributes()
Builds and returns a map of the element's attributes, else empty map if the element has no attributes.
-
children
public List<XmlElement> children()
Returns the element's children, else empty list if the element has no children.
-
element
public Element element()
Returns the underlying element.
-
get
public XmlElement get(int index)
Returns the XmlElement at theindex
, else null if none can be found or the node at theindex
is not an element
-
get
public XmlElement get(String tagName)
Returns the first child XmlElement matching thetagName
.- Specified by:
get
in classXmlSearchable<Element>
-
getAll
public List<XmlElement> getAll(String tagName)
Returns all child XmlElements matching thetagName
.- Specified by:
getAll
in classXmlSearchable<Element>
-
hasAttribute
public boolean hasAttribute(String attribute)
Returns whether the element contains theattribute
.
-
hasAttributes
public boolean hasAttributes()
Returns whether the element contains attributes.
-
hasChild
public boolean hasChild(String name)
Returns whether the element contains any child elements with thename
.
-
name
public String name()
Returns the element's name.
-
toXml
public String toXml()
Returns a XML representation of the element, including its attributes and value.
-
value
public String value()
Returns the text value of the element.
-
valueAsBoolean
public boolean valueAsBoolean()
Get the element value as a boolean. Returns true for "true", "1", "yes", "y" ignoring case, else returns false.
-
valueAsDouble
public double valueAsDouble()
Get the element value as a double.- Throws:
NumberFormatException
- if the value is not a valid double
-
valueAsEnum
public <V extends Enum<V>> V valueAsEnum(Class<V> targetEnum)
Get the element value as an enum of typeV
. Returns null if value cannot be parsed to an enum of typeV
.- Type Parameters:
V
- enum type
-
valueAsInt
public int valueAsInt()
Get the element value as an integer.- Throws:
NumberFormatException
- if the value is not a valid int
-
valueAsLong
public long valueAsLong()
Get the element value as a long.- Throws:
NumberFormatException
- if the value is not a valid long
-
-