Parse XML File to List of Object in SAX

Create new package named data. In this package, create new xml file named products.xml as below:

<?xml version="1.0" encoding="UTF-8"?>
<products>
	<product>
		<id>p01</id>
		<name>name 1</name>
		<price currency="usd">20</price>
		<quantity>5</quantity>
		<weight unit="kg">2.1</weight>
		<date format="dd/MM/yyyy">02/03/2017</date>
		<status>true</status>
	</product>
	<product>
		<id>p02</id>
		<name>name 2</name>
		<price currency="eur">12</price>
		<quantity>3</quantity>
		<weight unit="kg">6.5</weight>
		<date format="dd/MM/yyyy">24/11/2018</date>
		<status>true</status>
	</product>
	<product>
		<id>p03</id>
		<name>name 3</name>
		<price currency="aud">13</price>
		<quantity>1</quantity>
		<weight unit="kg">7.8</weight>
		<date format="dd/MM/yyyy">14/10/2018</date>
		<status>false</status>
	</product>
</products>

Create new package named entities. In this package, create new java class named Product.java as below:

package entities;

import java.util.Date;

public class Product implements java.io.Serializable {

	private String id;
	private String name;
	private double price;
	private int quantity;
	private double weight;
	private Date date;
	private boolean status;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

}




In models package, create new java class named ProductModel.java contains methods to interact with the xml file.

package models;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import entities.Product;

public class ProductModel {

	private List<Product> products;

	public List<Product> findAll() {
		try {
			SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
			SAXParser saxParser = saxParserFactory.newSAXParser();
			saxParser.parse("src\\data\\products.xml", new ProductDefaultHandler());
			return this.products;
		} catch (Exception e) {
			return null;
		}
	}

	private class ProductDefaultHandler extends DefaultHandler {

		private String tagName = "";
		private boolean isProductTag = false;
		private Product product;

		@Override
		public void startDocument() throws SAXException {
			products = new ArrayList<Product>();
		}

		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes)
				throws SAXException {
			this.tagName = qName;
			if (this.tagName.equalsIgnoreCase("product")) {
				this.isProductTag = true;
				this.product = new Product();
			}
		}

		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
			try {
				String content = new String(ch, start, length);
				if (this.tagName.equalsIgnoreCase("id")) {
					product.setId(content);
				}
				if (this.tagName.equalsIgnoreCase("name")) {
					product.setName(content);
				}
				if (this.tagName.equalsIgnoreCase("price")) {
					double price = Double.parseDouble(content);
					product.setPrice(price);
				}
				if (this.tagName.equalsIgnoreCase("quantity")) {
					int quantity = Integer.parseInt(content);
					product.setQuantity(quantity);
				}
				if (this.tagName.equalsIgnoreCase("weight")) {
					double weight = Double.parseDouble(content);
					product.setWeight(weight);
				}
				if (this.tagName.equalsIgnoreCase("date")) {
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
					Date date = simpleDateFormat.parse(content);
					product.setDate(date);
				}
				if (this.tagName.equalsIgnoreCase("status")) {
					boolean status = Boolean.parseBoolean(content);
					product.setStatus(status);
				}
				this.tagName = "";
			} catch (Exception e) {
				System.err.println(e.getMessage());
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			if (qName.equalsIgnoreCase("product")) {
				this.isProductTag = false;
				products.add(product);
			}
		}

	}

}

package demo;

import java.text.SimpleDateFormat;
import entities.Product;
import models.ProductModel;

public class Main {

	public static void main(String[] args) {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");
		ProductModel productModel = new ProductModel();
		for(Product product : productModel.findAll()) {
			System.out.println("Id: " + product.getId());
			System.out.println("Name: " + product.getName());
			System.out.println("Price: " + product.getPrice());
			System.out.println("Quantity: " + product.getQuantity());
			System.out.println("Status: " + product.isStatus());
			System.out.println("Date: " + simpleDateFormat.format(product.getDate()));
			System.out.println("============================");
		}

	}

}




Id: p01
Name: name 1
Price: 20.0
Quantity: 5
Status: true
Date: 03/02/2017
============================
Id: p02
Name: name 2
Price: 12.0
Quantity: 3
Status: true
Date: 11/24/2018
============================
Id: p03
Name: name 3
Price: 13.0
Quantity: 1
Status: false
Date: 10/14/2018
============================