Monday 30 May 2011

Reading an XML document using JDOM

ReadingxmlusingJdom.java
/*
 * @Program to read an XML document using JDOM?
 * ReadingxmlusingJdom.java
 * Author:-RoseIndia Team
 * Date:-10-Jun-2008
 */
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import java.io.ByteArrayInputStream;
import java.util.List;
public class ReadingxmlusingJdom {
  public static void main(String[] args) throws Exception {
  String data =
  "<root>" +
  "<Companyname>" +
  "<Employee name=\"Girish\" Age=\"25\">Developer</Employee>" +
  "</Companyname>" +
  "<Companyname>" +
  "<Employee name=\"Komal\" Age=\"25\">Administrator</Employee>" +
  "</Companyname>" +
  "</root>";
  SAXBuilder builder = new SAXBuilder();
  Document document = builder.build(
  new ByteArrayInputStream(data.getBytes()));
  Element root = document.getRootElement();
  List row = root.getChildren("Companyname");
  for (int i = 0; i < row.size(); i++) {
  Element Companyname = (Element) row.get(i);

  List column = Companyname.getChildren("Employee");
  for (int j = 0; j < column.size(); j++) {
 Element Employee = (Element) column.get(j);
 String name = Employee.getAttribute("name").getValue();
 String value = Employee.getText();
 int length = Employee.getAttribute("Age").getIntValue();
 System.out.println("Name = " + name);
 System.out.println("Profile = " + value);
 System.out.println("Age = " + length);
  }
  }
  }
}

Output of the program:-
Name = Girish
Profile = Developer
Age = 25
Name = Komal
Profile = Administrator
Age = 25

No comments:

Post a Comment