JDOM Attribute Example, Different method of Attribute class.


 

JDOM Attribute Example, Different method of Attribute class.

In this tutorial, you will see the use of different method of Attribute class.

In this tutorial, you will see the use of different method of Attribute class.

JDOM Attribute Example, Different method of Attribute class.

This tutorial explains the use of the  getParent() and getQualifiedName(). These methods are present in the Attribute class. The Attribute class is available part of JDOM library.

Example discussed in this section explains the use of getPatent() and getQualifiedName() methods. After reading the examples you will be able to apply the logic to use correctly in your program.

The getChild(java.lang.String name) method of Element class returns the child element.
The getParent() method of Attribute class returns parent of the associated attribute.
The getQualifiedName() method of Attribute class returns name of attribute.

We will be using the following XML file.

book.xml

<?xml version="1.0" encoding="UTF-8"?>
<BookCatalog>
    <book >
       <name>Java</name>
        <year>2000</year>
    </book>
    <book>
         <name>c</name>
         <year>1986</year>
   </book>
</BookCatalog>

AddAttributeList.java

package roseindia;
import java.io.File;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.DOMBuilder;

public class DifferentMethod {
  public static void main(String[] args) {
    try {
      String data = "book.xml";
      File file = new File(data);
      DOMBuilder domBuilder = new DOMBuilder();
      Document doc = domBuilder.build(file);
      Element rootEl = doc.getRootElement();
      Element attElement = rootEl.getChild("book");
     System.out.println("Child Name of root : " + attElement);
      attElement.setAttribute("id""98");
      Attribute attribute = attElement.getAttribute("id");
           Element parent = attribute.getParent();
      String attributeName = attribute.getQualifiedName();
      System.out.println("Parent of attribute : " + parent);
      System.out.println("Name of attribute : " 
    
+ attributeName);
    catch (Exception ex) {
      System.out.println("Execption " + ex);
    }  
}
}

Output:

Child Name of root: [Element: <book/>]
Parent of attribute : [Element: <book/>]
 Name of attribute : id

Download this code

Ads