
- 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 getAttributesSize() Method
The Java JDOM getAttributesSize() method of Element class is used to get the number of attributes that are attached to the current element. This method returns '0' if there are no attributes attached to the element.
Syntax
Following is the syntax of the Java JDOM Element getAttributesSize() method −
Element.getAttributesSize();
Parameters
The Java getAttributesSize() method doesn't accept any parameters.
Return Value
The Java getAttributesSize() method returns an integer value that represents number of attributes 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 getAttributesSize() method to get the number of attributes from the department element.
import java.io.File; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; public class GetAttributesSize { 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 size int attrSize = root.getAttributesSize(); System.out.println("Number of attributes of root : "+attrSize); } catch (Exception e) { e.printStackTrace(); } } }
The output window displays the number of attributes of department element.
Number of attributes of root : 2
Example 2
The getAttributesSize() method returns '0' if there are no attributes associated with the current XML element.
import org.jdom2.Document; import org.jdom2.Element; public class GetAttributesSize { 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 size int attrSize = root.getAttributesSize(); System.out.println("Number of attributes of root : "+attrSize); } catch (Exception e) { e.printStackTrace(); } } }
The number of attributes of the root element is displayed.
Number of attributes of root : 0