
- 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 getAttributeValue() Method
The Java JDOM getAttributeValue() method of Element class is used to get the attribute value of the supplied attribute. Using this method, we can get the value of an attribute within a particular namespace.
The getAttributeValue() method returns null if there is no such attribute that supplied as argument. If the attribute exists and value is not attached, it returns an empty string.
Syntax
Following is the syntax of the Java JDOM Element getAttributeValue() method −
Element.getAttributeValue(attname); Element.getAttributeValue(attname, def); Element.getAttributeValue(attname, ns); Element.getAttributeValue(attname, ns, def);
Parameters
The Java getAttributeValue() method is a polymorphic method and it accepts the following parameters −
- attname − attribute name whose value needs to be found.
- def − default value that should be returned if attribute doesn't exist.
- ns − Namespace that is attached to the attribute that needs to be found.
Return Value
The Java getAttributeValue() method returns value of the specified attribute in the form of a string.
Example 1
Here is the department.xml file that we need to parse −
<?xml version="1.0" encoding="UTF-8"?> <department xmlns:buz ="https://attribute/ns/buz" buz:id="101" code="CS" id="90"> <name>Computer Science</name> <staffCount>20</staffCount> </department>
The following basic java program explains the usage of Java JDOM Element getAttributeValue() method −
import java.io.File; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; public class GetAttributeValue { 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 attribute value String attrValue = root.getAttributeValue("id"); System.out.println("Department id : "+attrValue); } catch (Exception e) { e.printStackTrace(); } } }
Department id of the department element gets displayed.
Department id : 90
Example 2
The getAttributeValue() method can also be used to return a default value if the specified attribute doesn't exist for the Element.
import java.io.File; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; public class GetAttributeValue { 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 attribute value String attrValue = root.getAttributeValue("blockId","Not found"); System.out.println("Department Block id : "+attrValue); } catch (Exception e) { e.printStackTrace(); } } }
Department block id is displayed as not found.
Department Block id : Not found
Example 3
The getAttributeValue() method is used to get the attribute value of an attribute that is attached to a particular namespace.
import java.io.File; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.Namespace; import org.jdom2.input.SAXBuilder; public class GetAttributeValue { 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(); //create namespace object Namespace ns = Namespace.getNamespace("buz","https://attribute/ns/buz"); //Get attribute value String attrValue = root.getAttributeValue("id",ns); System.out.println("Department id with namespace 'buz': "+attrValue); } catch (Exception e) { e.printStackTrace(); } } }
The department id associated with the supplied namespace is displayed.
Department id with namespace 'buz': 101