Java JDOM Element setAttribute() Method



The Java JDOM setAttribute() method of Element class is used to set an attribute to the XML Element. Using this method, an attribute can be set with a specified value and also with a namespace.

The setAttribute() method throws an IllegalAddException if the namespace collides with another namespace on the same element. It throws an IllegalDataException if an illegal value is provided and IllegalNameException if the supplied name is illegal to be an attribute name or if a default namespace is provided without a prefix.

Syntax

Following is the syntax of the Java JDOM Element setAttribute() method −

Element.setAttribute(attribute);
Element.setAttribute(name, value);
Element.setAttribute(name, value, ns);

Parameters

The Java Element.setAttribute() method is a polymorphic method and it accepts the following parameters −

  • attribute − represents an Attribute object to add.
  • name − represents the name of an attribute.
  • value − represents the value of an attribute.
  • ns − represents the namespace that is attached to the attribute.

Return Value

The Java setAttribute() method returns the element to which the attribute gets added.

Example 1

Here is the basic example of using the Java JDOM Element setAttribute() method −

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class SetAttribute {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add root
	     Document doc = new Document();
	     Element root = new Element("book");
	     doc.setRootElement(root);
	     //Create attribute
	     Attribute attr = new Attribute("type", "single rule");
	     //Add attribute to root
	     root.setAttribute(attr);
	     //Print document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);	      
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The XML document after adding attribute to the root element is displayed.

<?xml version="1.0" encoding="UTF-8"?>
<book type="single rule" />

Example 2

The setAttribute() method takes the name and value of an attribute as arguments and sets them to the XML Element.

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class SetAttribute {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add root
	     Document doc = new Document();
	     Element root = new Element("book");
	     doc.setRootElement(root);
	     //Add attribute to root
	     root.setAttribute("type", "single rule");
	     //Print document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);	      
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The XML document after adding attribute to the root element is displayed.

<?xml version="1.0" encoding="UTF-8"?>
<book type="single rule" />

Example 3

The setAttribute() method sets the attribute name, value and also the namespace if provided as a third argument.

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class SetAttribute {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add root
	     Document doc = new Document();
	     Element root = new Element("book");
	     doc.setRootElement(root);
	     //Add attribute to root
	     Namespace ns = Namespace.getNamespace("pre","https://namespace/book");
	     root.setAttribute("type", "single rule",ns);
	     //Print document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);	      
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The XML document after adding attribute to the root element is displayed.

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns:pre="https://namespace/book" pre:type="single rule" />
Advertisements