
- Java XML Home
- Java XML Overview
- Java XML Parsers
- Java DOM Parser
- Java DOM Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java SAX Parser
- Java SAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- JDOM XML Parser
- JDOM XML Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java StAX Parser
- Java StAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XPath Parser
- Java XPath Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java DOM4J Parser
- Java DOM4J Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XML Useful Resources
- Java XML - Questions and Answers
- Java XML - Quick Guide
- Java XML - Useful Resources
- Java XML - Discussion
Java JDOM Element getAttributes() Method
The Java JDOM getAttributes() method of Element class retrieves all the attributes of an XML element as a list of Attribute objects. This method returns an empty list if there are no attributes.
The Java JDOM Element getAttributes() method returns a live list and hence any changes made to these attribute objects will affect the original attributes. So, it is advisable to use hasAttributes() or getAttributesSize() to know the basic information about the attributes of an element.
Syntax
Following is the syntax of the Java JDOM Element getAttributes() method −
Element.getAttributes();
Parameters
The Java getAttributes() method doesn't accept any parameters.
Return Value
The Java getAttributes() method returns the list of Attribute objects of an XML element.
Example 1
Here is the department.xml file that has two attributes.
<?xml version="1.0" encoding="UTF-8"?> <department id="101" code="CS"> <name>Computer Science</name> <staffCount>20</staffCount> </department>
The following Java program uses the Java JDOM Element getAttributes() method to get the list of attributes from the department element.
import java.io.File; import java.util.List; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; public class GetAttributes { public static void main(String args[]) { try { //Read the document and get the root SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("department.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); //Get attributes List<Attribute> attrList = root.getAttributes(); System.out.println(attrList); } catch (Exception e) { e.printStackTrace(); } } }
The list of attributes are displayed on the output screen.
[[Attribute: id="101"], [Attribute: code="CS"]]
Example 2
The getAttributes() method returns an empty list if there are no attributes for the XML element.
import java.util.List; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; public class GetAttributes { public static void main(String args[]) { try { //Create Document and set root Document doc = new Document(); Element root = new Element("root"); doc.setRootElement(root); //Get attributes List<Attribute> attrList = root.getAttributes(); System.out.println(attrList); } catch (Exception e) { e.printStackTrace(); } } }
An empty list is displayed on the output screen.
[]