Home Tutorial Xml Jdom JDOM Element Example, How to get attribute value of xml element.

 
 

Share on Google+Share on Google+
JDOM Element Example, How to get attribute value of xml element.
Posted on: August 18, 2010 at 12:00 AM
Advertisement
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

Advertisement

Related Tags for JDOM Element Example, How to get attribute value of xml element.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 18, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com