Package snap.util

Class XMLElement

java.lang.Object
snap.util.XMLElement
All Implemented Interfaces:
Cloneable

public class XMLElement extends Object implements 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 Details

    • XMLElement

      public XMLElement()
      Creates a new element.
    • XMLElement

      public XMLElement(String aName)
      Creates a new element with given name.
    • XMLElement

      public XMLElement(String aName, String aValue)
      Creates a new element with given name and value.
  • Method Details

    • getName

      public String getName()
      Returns the name for this attribute.
    • setName

      public void setName(String aName)
      Sets the name for this attribute.
    • getFullName

      public String getFullName()
      Returns the full name for this attribute.
    • setFullName

      public void setFullName(String aName)
      Sets the full name for this attribute.
    • getPrefix

      public String getPrefix()
      Returns the prefix if full name is different.
    • getValue

      public String getValue()
      Returns the value for this attribute.
    • setValue

      public void setValue(String aValue)
      Sets the value for this attribute.
    • getNamespace

      public String getNamespace()
      Returns the namespace.
    • setNamespace

      public void setNamespace(String aURI)
      Sets the namespace.
    • isProcInstr

      public boolean isProcInstr()
      Returns whether element is processing instruction.
    • getProcInstrData

      public String getProcInstrData()
      Returns the processing instruction.
    • setProcInstrData

      public void setProcInstrData(String aStr)
      Sets the processing instruction data.
    • getAttributeCount

      public int getAttributeCount()
      Returns the number of child attributes for this element.
    • getAttribute

      public XMLAttribute getAttribute(int anIndex)
      Returns the specific child attribute at the given index.
    • getAttributes

      public List<XMLAttribute> getAttributes()
      Returns the list of child attributes for this element.
    • addAttribute

      public void addAttribute(XMLAttribute anAttribute)
      Adds an attribute.
    • addAttribute

      public void addAttribute(XMLAttribute anAttribute, int anIndex)
      Adds an attribute at given index.
    • removeAttribute

      public XMLAttribute removeAttribute(int anIndex)
      Removes the attribute at given index.
    • removeAttribute

      public int removeAttribute(XMLAttribute anAttribute)
      Removes the given attribute.
    • removeAttribute

      public XMLAttribute removeAttribute(String aName)
      Removes the attribute with given name.
    • getAttributeIndex

      public int getAttributeIndex(XMLAttribute anAttribute)
      Returns this index of the given attribute.
    • getAttribute

      public XMLAttribute getAttribute(String aName)
      Returns the specific child attribute with the given name (or null if not found).
    • getAttributeIndex

      public int getAttributeIndex(String aName)
      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

      public XMLElement get(int anIndex)
      Returns the specific child element at the given index.
    • get

      public XMLElement get(String aName)
      Returns the first child element with the given name.
    • getElementCount

      public int getElementCount()
      Returns the number of child elements.
    • getElement

      public XMLElement getElement(int anIndex)
      Returns the individual element at given index.
    • getElements

      public List<XMLElement> getElements()
      Returns the list of elements.
    • addElement

      public void addElement(XMLElement anElement)
      Adds given element to elements list.
    • addElement

      public void addElement(XMLElement anElement, int anIndex)
      Adds given element to elements list at given index.
    • removeElement

      public XMLElement removeElement(int anIndex)
      Removes element at given index.
    • removeElement

      public int removeElement(XMLElement anElement)
      Removes given element.
    • getElementIndex

      public int getElementIndex(XMLElement anElement)
      Returns the index of the given element.
    • getElementCount

      public int getElementCount(String aName)
      Returns the number of child elements with the given name.
    • getElementIndex

      public int getElementIndex(String aName, int start)
      Returns the index of element with given name.
    • getElement

      public XMLElement getElement(String aName)
      Returns the first element for a given name.
    • removeElement

      public XMLElement removeElement(String aName)
      Removes the first element with given name and returns it.
    • getElements

      public List<XMLElement> getElements(String aName)
      Returns the list of child elements with given name.
    • removeElements

      public List<XMLElement> removeElements(String aName)
      Removes elements for given element name.
    • hasAttribute

      public boolean hasAttribute(String aName)
      Checks for presence of an attribute.
    • getAttributeValue

      public String getAttributeValue(String aName)
      Returns the attribute string value for the given attribute name.
    • getAttributeValue

      public String getAttributeValue(String aName, String defaultValue)
      Returns the string value for the given attribute name (or the given default value, if name not found).
    • getAttributeBoolValue

      public boolean getAttributeBoolValue(String aName)
      Returns the boolean value for the given attribute name.
    • getAttributeBoolValue

      public boolean getAttributeBoolValue(String aName, boolean defaultValue)
      Returns the boolean value for the given attribute name (or the given default value, if name not found).
    • getAttributeBooleanValue

      public Boolean getAttributeBooleanValue(String aName)
      Returns the Boolean value for the given attribute name.
    • getAttributeBooleanValue

      public Boolean getAttributeBooleanValue(String aName, Boolean defaultValue)
      Returns the Boolean value for the given attribute name (or the given default value, if name not found).
    • getAttributeIntValue

      public int getAttributeIntValue(String aName)
      Returns the int value for the given attribute name.
    • getAttributeIntValue

      public int getAttributeIntValue(String aName, int defaultValue)
      Returns the int value for the given attribute name (or the given default value, if name not found).
    • getAttributeFloatValue

      public float getAttributeFloatValue(String aName)
      Returns the float value for the given attribute name.
    • getAttributeFloatValue

      public float getAttributeFloatValue(String aName, float defaultValue)
      Returns the float value for the given attribute name (or the given default value, if name not found).
    • getAttributeDoubleValue

      public double getAttributeDoubleValue(String aName)
      Returns the double value for the given attribute name.
    • getAttributeDoubleValue

      public double getAttributeDoubleValue(String aName, double defaultValue)
      Returns the double value for the given attribute name (or the given default value, if name not found).
    • getAttributeNumberValue

      public Number getAttributeNumberValue(String aName)
      Returns the Number value for the given attribute name.
    • getAttributeNumberValue

      public Number getAttributeNumberValue(String aName, Number defaultValue)
      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

      public XMLElement add(String aName, String aValue)
      Adds a new attribute with the given name and string value.
    • add

      public XMLElement add(String aName, Object aValue)
      Adds a new attribute with the given name using the object's toString() method.
    • add

      public XMLElement add(String aName, boolean aValue)
      Adds a new attribute with the given name and boolean value.
    • add

      public XMLElement add(String aName, Boolean aValue)
      Adds a new attribute with the given name and Boolean value.
    • add

      public XMLElement add(String aName, int aValue)
      Adds a new attribute with the given name and int value.
    • add

      public XMLElement add(String aName, double aValue)
      Adds a new attribute with the given name and float value.
    • add

      public XMLElement add(XMLAttribute anAttribute)
      Adds the given attribute to this element's list of attributes.
    • add

      public XMLElement add(XMLElement anElement)
      Adds the given attribute to this element's list of attributes.
    • addAll

      public void addAll(XMLElement anElement)
      Adds all the given child elements and attributes of given element to this element.
    • indexOf

      public int indexOf(String aName)
      Returns the index of the first child element with the given name.
    • indexOf

      public int indexOf(String aName, int startIndex)
      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

      protected boolean equals(String aStr1, String aStr2)
      A string compare that honors CaseSensitive setting.
    • clone

      public XMLElement clone()
      Returns a clone of this element.
      Overrides:
      clone in class Object
    • getString

      public String getString()
      Returns a string representation of this element (XML).
    • toString

      public String toString()
      Returns a string representation of this element (XML).
      Overrides:
      toString in class Object
    • getBytes

      public byte[] getBytes()
      Returns XML bytes for this element.
    • write

      public void write(StringBuffer aSB, String indentStr)
      Writes the element to the given string buffer using the given indent level and indent string.
    • write

      public 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.
    • readFromXMLSource

      public static XMLElement readFromXMLSource(Object aSource)
      Returns XML loaded from aSource (File, String path, InputStream or whatever).
    • readFromXMLString

      public static XMLElement readFromXMLString(String aString)
      Returns XML loaded from XML String.
    • readFromXMLBytes

      public static XMLElement readFromXMLBytes(byte[] theBytes)
      Returns XML loaded from XML bytes.