Package snap.util
Class XMLElement
java.lang.Object
snap.util.XMLElement
- All Implemented Interfaces:
Cloneable
The class represents an XML element and simply manages a list of XML attributes and a list XML elements (recursively).
For the sake of efficiency, when you need to iterate over a list of elements for a given name, you can do this:
for (int i=anElement.indexOf(elementName); i>=0; i=anElement.indexOf(elementName, i+1))
anElement.get(i).doThis();
-
Constructor Summary
ConstructorDescriptionCreates a new element.XMLElement
(String aName) Creates a new element with given name.XMLElement
(String aName, String aValue) Creates a new element with given name and value. -
Method Summary
Modifier and TypeMethodDescriptionAdds a new attribute with the given name and boolean value.Adds a new attribute with the given name and float value.Adds a new attribute with the given name and int value.Adds a new attribute with the given name and Boolean value.Adds a new attribute with the given name using the object's toString() method.Adds a new attribute with the given name and string value.add
(XMLAttribute anAttribute) Adds the given attribute to this element's list of attributes.add
(XMLElement anElement) Adds the given attribute to this element's list of attributes.void
addAll
(XMLElement anElement) Adds all the given child elements and attributes of given element to this element.void
addAttribute
(XMLAttribute anAttribute) Adds an attribute.void
addAttribute
(XMLAttribute anAttribute, int anIndex) Adds an attribute at given index.void
addElement
(XMLElement anElement) Adds given element to elements list.void
addElement
(XMLElement anElement, int anIndex) Adds given element to elements list at given index.clone()
Returns a clone of this element.protected boolean
A string compare that honors CaseSensitive setting.get
(int anIndex) Returns the specific child element at the given index.Returns the first child element with the given name.getAttribute
(int anIndex) Returns the specific child attribute at the given index.getAttribute
(String aName) Returns the specific child attribute with the given name (or null if not found).getAttributeBooleanValue
(String aName) Returns the Boolean value for the given attribute name.getAttributeBooleanValue
(String aName, Boolean defaultValue) Returns the Boolean value for the given attribute name (or the given default value, if name not found).boolean
getAttributeBoolValue
(String aName) Returns the boolean value for the given attribute name.boolean
getAttributeBoolValue
(String aName, boolean defaultValue) Returns the boolean value for the given attribute name (or the given default value, if name not found).int
Returns the number of child attributes for this element.double
getAttributeDoubleValue
(String aName) Returns the double value for the given attribute name.double
getAttributeDoubleValue
(String aName, double defaultValue) Returns the double value for the given attribute name (or the given default value, if name not found).<T extends Enum<T>>
TgetAttributeEnumValue
(String aName, Class<T> enumClass, T defaultValue) Returns the Enum of a specific type for the given attribute name (or the given default value, if name not found).float
getAttributeFloatValue
(String aName) Returns the float value for the given attribute name.float
getAttributeFloatValue
(String aName, float defaultValue) Returns the float value for the given attribute name (or the given default value, if name not found).int
getAttributeIndex
(String aName) Returns the index of the attribute with the given name (or -1 if not found).int
getAttributeIndex
(XMLAttribute anAttribute) Returns this index of the given attribute.int
getAttributeIntValue
(String aName) Returns the int value for the given attribute name.int
getAttributeIntValue
(String aName, int defaultValue) Returns the int value for the given attribute name (or the given default value, if name not found).getAttributeNumberValue
(String aName) Returns the Number value for the given attribute name.getAttributeNumberValue
(String aName, Number defaultValue) Returns the Number value for the given attribute name (or the given default value, if name not found).Returns the list of child attributes for this element.getAttributeValue
(String aName) Returns the attribute string value for the given attribute name.getAttributeValue
(String aName, String defaultValue) Returns the string value for the given attribute name (or the given default value, if name not found).byte[]
getBytes()
Returns XML bytes for this element.getElement
(int anIndex) Returns the individual element at given index.getElement
(String aName) Returns the first element for a given name.int
Returns the number of child elements.int
getElementCount
(String aName) Returns the number of child elements with the given name.int
getElementIndex
(String aName, int start) Returns the index of element with given name.int
getElementIndex
(XMLElement anElement) Returns the index of the given element.Returns the list of elements.getElements
(String aName) Returns the list of child elements with given name.Returns the full name for this attribute.getName()
Returns the name for this attribute.Returns the namespace.Returns the prefix if full name is different.Returns the processing instruction.Returns a string representation of this element (XML).getValue()
Returns the value for this attribute.byte[]
Returns the element value as bytes.boolean
hasAttribute
(String aName) Checks for presence of an attribute.int
Returns the index of the first child element with the given name.int
Returns the index of the first child element with the given name at or beyond the given index.boolean
Returns whether element should ignore case when asking for attributes/elements by name.boolean
Returns whether element is processing instruction.static XMLElement
readFromXMLBytes
(byte[] theBytes) Returns XML loaded from XML bytes.static XMLElement
readFromXMLSource
(Object aSource) Returns XML loaded from aSource (File, String path, InputStream or whatever).static XMLElement
readFromXMLString
(String aString) Returns XML loaded from XML String.removeAttribute
(int anIndex) Removes the attribute at given index.removeAttribute
(String aName) Removes the attribute with given name.int
removeAttribute
(XMLAttribute anAttribute) Removes the given attribute.removeElement
(int anIndex) Removes element at given index.removeElement
(String aName) Removes the first element with given name and returns it.int
removeElement
(XMLElement anElement) Removes given element.removeElements
(String aName) Removes elements for given element name.void
setFullName
(String aName) Sets the full name for this attribute.void
setIgnoreCase
(boolean aVal) Sets whether element should ignore case when asking for attributes/elements by name.void
Sets the name for this attribute.void
setNamespace
(String aURI) Sets the namespace.void
setProcInstrData
(String aStr) Sets the processing instruction data.void
Sets the value for this attribute.void
setValueBytes
(byte[] theBytes) Sets the element value from bytes.int
size()
Returns child element list size.toString()
Returns a string representation of this element (XML).void
write
(StringBuffer aSB, String indentStr) Writes the element to the given string buffer using the given indent level and indent string.void
write
(StringBuffer aSB, String aNameSpace, int indent, String indentStr) Writes the element to the given string buffer using the given indent level and indent string.
-
Constructor Details
-
XMLElement
public XMLElement()Creates a new element. -
XMLElement
Creates a new element with given name. -
XMLElement
Creates a new element with given name and value.
-
-
Method Details
-
getName
Returns the name for this attribute. -
setName
Sets the name for this attribute. -
getFullName
Returns the full name for this attribute. -
setFullName
Sets the full name for this attribute. -
getPrefix
Returns the prefix if full name is different. -
getValue
Returns the value for this attribute. -
setValue
Sets the value for this attribute. -
getNamespace
Returns the namespace. -
setNamespace
Sets the namespace. -
isProcInstr
public boolean isProcInstr()Returns whether element is processing instruction. -
getProcInstrData
Returns the processing instruction. -
setProcInstrData
Sets the processing instruction data. -
getAttributeCount
public int getAttributeCount()Returns the number of child attributes for this element. -
getAttribute
Returns the specific child attribute at the given index. -
getAttributes
Returns the list of child attributes for this element. -
addAttribute
Adds an attribute. -
addAttribute
Adds an attribute at given index. -
removeAttribute
Removes the attribute at given index. -
removeAttribute
Removes the given attribute. -
removeAttribute
Removes the attribute with given name. -
getAttributeIndex
Returns this index of the given attribute. -
getAttribute
Returns the specific child attribute with the given name (or null if not found). -
getAttributeIndex
Returns the index of the attribute with the given name (or -1 if not found). -
size
public int size()Returns child element list size. -
get
Returns the specific child element at the given index. -
get
Returns the first child element with the given name. -
getElementCount
public int getElementCount()Returns the number of child elements. -
getElement
Returns the individual element at given index. -
getElements
Returns the list of elements. -
addElement
Adds given element to elements list. -
addElement
Adds given element to elements list at given index. -
removeElement
Removes element at given index. -
removeElement
Removes given element. -
getElementIndex
Returns the index of the given element. -
getElementCount
Returns the number of child elements with the given name. -
getElementIndex
Returns the index of element with given name. -
getElement
Returns the first element for a given name. -
removeElement
Removes the first element with given name and returns it. -
getElements
Returns the list of child elements with given name. -
removeElements
Removes elements for given element name. -
hasAttribute
Checks for presence of an attribute. -
getAttributeValue
Returns the attribute string value for the given attribute name. -
getAttributeValue
Returns the string value for the given attribute name (or the given default value, if name not found). -
getAttributeBoolValue
Returns the boolean value for the given attribute name. -
getAttributeBoolValue
Returns the boolean value for the given attribute name (or the given default value, if name not found). -
getAttributeBooleanValue
Returns the Boolean value for the given attribute name. -
getAttributeBooleanValue
Returns the Boolean value for the given attribute name (or the given default value, if name not found). -
getAttributeIntValue
Returns the int value for the given attribute name. -
getAttributeIntValue
Returns the int value for the given attribute name (or the given default value, if name not found). -
getAttributeFloatValue
Returns the float value for the given attribute name. -
getAttributeFloatValue
Returns the float value for the given attribute name (or the given default value, if name not found). -
getAttributeDoubleValue
Returns the double value for the given attribute name. -
getAttributeDoubleValue
Returns the double value for the given attribute name (or the given default value, if name not found). -
getAttributeNumberValue
Returns the Number value for the given attribute name. -
getAttributeNumberValue
Returns the Number value for the given attribute name (or the given default value, if name not found). -
getAttributeEnumValue
public <T extends Enum<T>> T getAttributeEnumValue(String aName, Class<T> enumClass, T defaultValue) Returns the Enum of a specific type for the given attribute name (or the given default value, if name not found). -
add
Adds a new attribute with the given name and string value. -
add
Adds a new attribute with the given name using the object's toString() method. -
add
Adds a new attribute with the given name and boolean value. -
add
Adds a new attribute with the given name and Boolean value. -
add
Adds a new attribute with the given name and int value. -
add
Adds a new attribute with the given name and float value. -
add
Adds the given attribute to this element's list of attributes. -
add
Adds the given attribute to this element's list of attributes. -
addAll
Adds all the given child elements and attributes of given element to this element. -
indexOf
Returns the index of the first child element with the given name. -
indexOf
Returns the index of the first child element with the given name at or beyond the given index. -
getValueBytes
public byte[] getValueBytes()Returns the element value as bytes. -
setValueBytes
public void setValueBytes(byte[] theBytes) Sets the element value from bytes. -
isIgnoreCase
public boolean isIgnoreCase()Returns whether element should ignore case when asking for attributes/elements by name. -
setIgnoreCase
public void setIgnoreCase(boolean aVal) Sets whether element should ignore case when asking for attributes/elements by name. -
equals
A string compare that honors CaseSensitive setting. -
clone
Returns a clone of this element. -
getString
Returns a string representation of this element (XML). -
toString
Returns a string representation of this element (XML). -
getBytes
public byte[] getBytes()Returns XML bytes for this element. -
write
Writes the element to the given string buffer using the given indent level and indent string. -
write
Writes the element to the given string buffer using the given indent level and indent string. -
readFromXMLSource
Returns XML loaded from aSource (File, String path, InputStream or whatever). -
readFromXMLString
Returns XML loaded from XML String. -
readFromXMLBytes
Returns XML loaded from XML bytes.
-