Parse an XML document using SAX parser

Simple API for XML(SAX) is an event driven parser which reads XML step by step. SAX Parser is faster and uses less memory than DOM parser.

Event handling methods used:
startElement() – Receive notification of the start of an element.
endElement() – Receive notification of the end of an element.
characters() – Receive notification of character data inside an element.

XML document to be parsed

        <name>The Terminal</name>
        <description>terminal foo foo foo</description>
        <name>Shutter Island</name>
        <description>shutter foo foo foo </description>
        <name>Old Boy</name>
        <description>old foo foo foo </description>

Create Model object

public class Movie {

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getImgURL() {
		return imgURL;

	public void setImgURL(String imgURL) {
		this.imgURL = imgURL;

	public String getDescription() {
		return description;

	public void setDescription(String description) {
		this.description = description;

	private String name;
	private String imgURL;
	private String description;

	public String toString() {
		return + " " + this.imgURL + " " + this.description;


Extend DefaultHandler and override above mentioned event handling methods.

public class MovieHandler extends DefaultHandler {
    List<Movie> movieList = new ArrayList<Movie>();
    Movie movie = null;
    String data = null;
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
               // When start tag of 'movie' element found, create an object and add it to the list
        switch (qName) {
        case "movie":
            movie = new Movie();
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        switch (qName) {
        case "name":
        case "description":
        case "image":
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        data = String.copyValueOf(ch, start, length).trim();

Set up the parser and get it started

public class MovieSaxParser {
    static final Logger LOG = Logger.getLogger(MovieSaxParser.class);
    String static fileLocation = "/home/prnv/movie_list.xml";
    public static void main(String[] args) {
        try {
            SAXParserFactory saxFactory = SAXParserFactory.newInstance();
            SAXParser parser = saxFactory.newSAXParser();
            MovieHandler handler = new MovieHandler();
            parser.parse(fileLocation, handler);
            for (Movie movie : handler.movieList) {
        } catch (ParserConfigurationException | SAXException | IOException e) {
            LOG.error("Exception ", e);

Backup files in EC2 Instance to Amazon S3

EC2 Instance : Ubuntu Server 12.04 LTS
Tool : s3cmd

Install s3cmd:

sudo apt-get install s3cmd

Configure s3cmd for managing data on S3:
Get the access key, secret key from your AWS account prior to this step.

s3cmd --configure

Listing buckets:

s3cmd ls

Create a bucket:

s3cmd mb s3://bucket_name

Upload files to S3:

s3cmd put -r ~/backup_directory/ s3://bucket_name/

Synchronize files:

s3cmd sync -r ~/backup_directory/ s3://bucket_name/

Download files from a bucket:

s3cmd get -r s3://bucket_name/ ~/my_s3_downloads/

Delete files in a bucket:

s3cmd del -r s3://bucket_name/

Remove a bucket:

s3cmd rb -f s3://bucket_name

Write a shell script to synchronize files in the EC2 Instance:

s3cmd sync -r ~/backup_directory/ s3://bucket_name/

Edit crontab file:

crontab -e

Schedule script:
Add cronjob to trigger automated backup everyday at 23:59 hrs

59 23 * * * sh ~/