Apache Common's Digester Examples

Digester is a very usefull api for parsing -xml documents.It acts like wrapper around  sax and dom parser and is quite simple once the rules are defined.
It was orginally developed to parse the struts.xml file.
There are two ways for defining the rules for parsing:
  1. define rules in the java
  2. define rules in the xml file.
Steps:


Analyse the xml file

Create Java Class

Create an Object Digester class in CustomDigester

Define rules in the Digester class

Parse the xml file

XML file dtd is avialable -in digester api-digester-rules.dtd
To get short training and examples on apache digester.leave a Comment with your e-mail id--below.


 Each example is unique and gives good insight into the digester package.

  package com.anjana.service;

import java.io.IOException;

import org.apache.commons.digester.Digester;

import org.apache.commons.digester.xmlrules.DigesterLoader;

import org.xml.sax.SAXException;

public class DigesterXMLJavaStudents {

public static void main( String[] args ) throws IOException, SAXException {

DigesterXMLJavaStudents xmlDigest= new DigesterXMLJavaStudents ();

xmlDigest.digest();

}

public void digest() throws IOException, SAXException{

//Create Digester using rules defined in studentRules.xml

Digester digester = DigesterLoader.createDigester(this.getClass().getClassLoader().getResource("studentRules.xml"));

//Parse academy.xml using the Digester to get an instance of Academy

Student student = (Student)digester.parse(this.getClass().getClassLoader().getResourceAsStream("Student.xml"));

}



}

Comments

Post a Comment

Popular posts from this blog

Spring MVC- Redirect from one controller to another

Jquery Table Inline Edititing