Home Tutorial Xml Jdom JDOM Attribute Example, Different method of Attribute class.

 
 

Share on Google+Share on Google+
JDOM Attribute Example, Different method of Attribute class.
Posted on: August 20, 2010 at 12:00 AM
Advertisement
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

Advertisement

Related Tags for JDOM Attribute Example, Different method of Attribute class.:


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 20, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com