JDOM Element Example, How to get attribute value of xml element.
In this tutorial, you will see how to get attribute value of xml element.
In this tutorial, you will see how to get attribute value of xml element.
JDOM Element Example, How to get
attribute value of xml element.
In this tutorial, we will see how to get attribute value of xml element..
Element API.
Return Type |
Method |
Description |
Attribute |
getAttribute(String name) |
The getAttribute() method returns attribute
value of given element. |
Code.
Student.xml
<?xml version="1.0"?>
<Student name="bharat">
<name id="1" >
<first name="one"> </first>
<last></last>
<age></age>
</name>
<name id="2">
<first id="two" ></first>
<last></last>
<age></age>
</name>
<name id="3" >
<first id="three"></first>
<last></last>
<age></age>
</name>
</Student>
|
GetAttribute.java
import java.io.File;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.DOMBuilder;
public class GetAttribute {
public static void main(String[] ar) {
try {
File XmlFile = new File("student.xml");
DOMBuilder db = new DOMBuilder(false);
Document dc = db.build(XmlFile);
Element e = dc.getRootElement();
System.out.println("Root " + e);
Attribute att = e.getAttribute("name");
System.out.println("Get root element Attribute." + att);
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
}
}
}
|
Output.
Root [Element: <Student/>]
Get root element Attribute.[Attribute: name="bharat"]
|
Download this code