Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Element Interface

Up to this point, we have examined the top of an XML tree, the Document interface, and the bottom of the tree—the Node interface. We will now examine the intermediate levels of the tree, which are represented by the Element interface.

Element objects form the intermediary branches of the XML tree. Elements make up the bulk of what a DOM document is. The primary purpose of the Element interface is to support the manipulation of attributes, and many of its methods are designed with this in mind. And, as always, the Element interface is derived from the Node interface and inherits all of its public methods.

The Element interface performs three major functions, allowing you to do the following:

  • Access and manipulate the attributes of the Element

  • Access the children of the Element

  • Access the tag of the Element

And, of course, the Element object always has node type ELEMENT_NODE.

Methods that Return Information About an Element

There is only one informational method on the Element interface.

String getTagName(); returns the tag associated with this element.

Methods for Manipulating Element Attributes

There are a number of methods for manipulating element attributes, the most common of which are listed below.

  • String getAttribute(String name); Returns the value of the given attribute.

  • Attr getAttributeNode(String name) Returns an Attr object that represents the given attribute.

  • void removeAttribute(String name) Removes the given attribute from the Element. For example: If an XML element contained two attributes, <price discount="retail" cur="us">9.95</price> and <price discount="wholesale" cur="us">7.95</price>, removeAttribute("retail"); could remove the first.

  • Attr removeAttributeNode(Attr someAttribute) Remove the given Attribute object from the Element.

  • void setAttribute(String name, String value) Sets or replaces the attribute name with the given value.

  • Attr setAttributeNode(Attr newAttribute); Adds a new Attribute object as a child of the current Element.

Note

setAttribute expects its value to be literal text that will need to be escaped, if required, when written. If you want to add an attribute that contains an EntityReference you need to create an Attr object and populate it and its children by setting the name, value, and so on. Then insert that attribute into the element.

Accessing Child Nodes

Element nodes contain one special method for accessing their child nodes.

The NodeList getElementsByTagName(String tag) method returns a NodeList of all the nodes that match the given tag. This method is identical in function to the method of the same name in the Document interface.

One additional method exists in Elementnormalize(). The normalize() method handles a special case that occurs when two or more text nodes are added as children of the same element. Say we have a <title> element, "Better," and we want to add two or more text nodes as children of the title, "Living" and "Through Chemistry." normalize would concatenate all three of these together resulting in a single text node representing the value of the element in question, which in our case would be "Better Living Through Chemistry."

  • + Share This
  • 🔖 Save To Your Account