Class XMLElement

  • All Implemented Interfaces:
    IXMLUtilsConstants

    public class XMLElement
    extends Object
    implements IXMLUtilsConstants
    Represents a simple XML element that may have given attributes, child elements or a string value. Note that if the delete method is called on an element, the element will disallow any further method invocations by throwing IllegalStateExceptions.
    • Method Detail

      • delete

        public void delete()
                    throws Exception
        Deletes this element from the master document. Once an element has been deleted, any further method invocations on this element will result in IllegalStateException being generated.
        Throws:
        Exception - if the delete operation fails
      • getAttributeValue

        public String getAttributeValue​(String name)
        Returns the value of the attribute with the given name as present in the current element. Note that this value may be null if the given attribute is not already present in this document. Also note that even though the attributes are defined with leading and trailing double quotes, the value returned by this method will be devoid of these quotes.
        Parameters:
        name - the name of the attribute whoes value must be returned.
        Returns:
        the value of given named attribute, or null if the attribute is not present.
      • addChildElementAt

        public void addChildElementAt​(XMLElement child,
                                      int index)
                               throws Exception
        Adds the given child element at the given index.
        Parameters:
        child -
        index -
        Throws:
        Exception
      • addChildElementAt

        public void addChildElementAt​(XMLElement child,
                                      int index,
                                      boolean addAfterNewLine)
                               throws Exception
        Adds the given child element at the specified index. If the index is out of bounds, this method will throw an exception.
        Parameters:
        child - the element to be added as a child of this element
        index - the index at which the child element has to be added
        Throws:
        Exception - if the addition operation fails
      • addChildElement

        public void addChildElement​(XMLElement child,
                                    boolean addAfterNewLine)
                             throws Exception
        Appends the given child element to the end of the list of existing child elements.
        Parameters:
        child -
        addAfterNewLine -
        Throws:
        Exception
      • addChildElement

        public void addChildElement​(XMLElement child)
                             throws Exception
        Appends the given child element to the end of the list of existing child elements.
        Parameters:
        child - the child element to append
        Throws:
        Exception - if the addition operation fails.
      • getNamedChildElements

        public ArrayList getNamedChildElements​(String name)
        Returns a list of named chiled elements of this element.
        Parameters:
        name - the name of the child elements which must be returned
        Returns:
        the child elements with the given names.
      • updateValue

        public void updateValue​(String value)
                         throws Exception
        Updates the value of this element. This method will fail with an exception if the current element does not store any value.
        Parameters:
        value - to be updated
        Throws:
        Exception - if the update operation fails
      • updateAttribute

        public void updateAttribute​(String name,
                                    String value)
        Updates the value of the an attribute identified by the given name. If this attribute is not present, the value is added to the attribute list at the end of the already present attributes. Note that it is not possible to add an attribute in the middle of the attribute list if already certain other attributes are present. One way to work around this limitation is to remove all attributes from this element and then add them in the required order as necessary.
        Parameters:
        name - the name of the attribute to be updated or added
        value - the value of the attirbute
        Throws:
        Exception - if the update was not successful
      • removeAttribute

        public int removeAttribute​(String name)
        Removes any attribute with the given name from this element. The return value indicates the number of attributes that were removed. If the give attribute is not present, the return value is 0.
        Parameters:
        name - the name of the attribute that must be removed.
        Returns:
        the number of attributes that were removed
      • toString

        public String toString()
        Returns an indented string representation of this element and any contained child elements.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this element along with its sub-tree
      • toXMLString

        public String toXMLString()
        Returns an xml fragment that represents this element and any contained child elements.
        Returns:
        an xml fragment representing this element
      • getValue

        public String getValue()
        Returns the value stored in this element.
        Returns:
        the value stored in this element
      • getName

        public String getName()
        Returns the name of this element
        Returns:
        the name of this element
      • getChildElements

        public ArrayList getChildElements()
        Returns an ordered collection of child elements of this element
        Returns:
        the collection of child elements in the order they are defined
      • getPath

        public String getPath()
        Returns a string representing the path of this XMLElement in the current document.
        Returns:
        the path string