Java JDOM Element Class



The Java JDOM Element class represents an XML Element. This class provides various methods to access the information of an element, that includes name, text content, attributes, child elements, namespaces, comments etc.

Using the methods of Element class, we can add, delete and modify content objects. All the attributes of an element can be obtained as Attribute objects and the namespaces as Namespace objects.

Constructors of Element Class

To create an Element object, the Element class provides the following default and parameterized constructors −

  • Element() − protected constructor used by subclasses to have control on variable initialization.
  • Element(java.lang.String name) − creates an Element with the specified name.
  • Element(java.lang.String name, Namespace namespace) − creates an Element with the specified name and Namespace.
  • Element(java.lang.String name, java.lang.String uri) − creates an Element with the specified name and the supplied URI as Namespace.
  • Element(java.lang.String name, java.lang.String prefix, java.lang.String uri) − creates an Element with the specified name, prefix and URI as Namespace.

Creating an XML Element

Using the above mentioned constructors, we can create XML Elements. Here is a Java program that uses constructors to create two XML elements.

import org.jdom2.Element;

public class CreateElement {
   public static void main(String args[]) {
      try {	
	     //Creating element
	     Element element1 = new Element("element");
	     System.out.println(element1);
	     //Creating element with namespace
	     Element element2 = new Element("element","https://element/constructor");
	     System.out.println(element2);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the two elements created using constructors.

[Element: <element/>]
[Element: <element [Namespace: https://element/constructor]/>]

Add Content

Inside an XML element, we can add child elements and comments. Following are some of the methods that help us add content objects inside an XML Element −

Method Description
addContent() Adds the content objects to the XML Element.
addNamespaceDeclaration() Adds Namespace to the XML Element.

Operations on Namespaces

The XML namespaces can be added, removed, retrieved and altered using the following methods −

Method Description
getNamespace() Retrieves Namespace declaration of the XML Element.
getNamespacePrefix() Retrieves the Namespace prefix of an XML Element.
removeNamespaceDeclaration() Removes Namespace from the XML Element.

Operations on Attributes

XML attributes are used to add extra information to an XML Element. Using the following methods, we can retrieve and remove the attributes of an XML element.

Method Description
removeAttribute() Removes attributes from an XML Element.
getAttributes() Returns the list of attributes of an XML Element.
getAttributesSize() Returns the number of attributes of an XML Element.
getAttributeValue() Returns the value of the specified attribute of an XML Element.

Retrieve Child Elements

The information of child elements such as the number of children, index of a child and text content of a child can be obtained using the following methods −

Method Description
indexOf() Returns the index of the child element.
getChildren() Returns the list of child elements.
getChildText() Returns the text content of a child element.

Get Content

The content objects such as child elements and comments inside an XML Element can be retrieved using the following methods −

Method Description
getContent() Returns the list of content objects inside this Element's scope.
getContentSize() Returns the number of content objects inside the current element.

Get Element Name and Text Content

The name of the element and the text content can be obtained by using the following methods −

Method Description
getName() Returns the local name of the XML Element.
getText() Returns the text content of an XML Element.
getTextNormalize() Returns the normalized text content of an XML Element.
getTextTrim() Returns the text content by removing white spaces at the beginning and the end.

Remove Child Elements

The child elements inside an XML element can be removed using the following methods −

Method Description
removeChildren() Removes the child elements one level deep from the Element's scope.
removeContent() Removes child content objects from the Element's scope.

Set Attributes

To set the attributes of an XML element, we can use the following methods −

Method Description
setAttribute() Sets an attribute for an XML Element.
setAttributes() Sets the list of attributes for an XML Element.

Set Content

Using the following methods, we can set the name of an element, content objects such as child elements, comments, namespaces and text content.

Method Description
setContent() Sets the content objects for the current element.
setName() Sets the name of an XML Element.
setNamespace() Sets the namespace of an XML Element.
setText() Sets the text content of an XML Element.

Other Methods

Following are few more methods of Element class that has different functionality −

Method Description
detach() Detaches the current element from it's parent.
isAncestor() Checks if the current element is an ancestor of the supplied element.
isRootElement() Checks if the current element is a root element of the specified element.
toString() Returns the current element in the form of a string
Advertisements