Author: Hussein Terek

How to read XML using SAX parser

In the previous article we talked about DOM parser and provided different examples for parsing and reading elements of an XML document. SAX parser is yet another XML parser provided by JDK which parses documents in a more optimized and faster way. SAX parser doesn’t load the whole document into the memory, however it parses the document line by line...

How to create XML file in Java

In this tutorial, we show how to create a new XML file using DOM parser provided by JDK. We’re going to create an XML called students.xml which lists students along with their basic information. 1- Create XML using Transformer The following code snippet appends 3 student elements to a root element called students and then transforms the document object to...

How to read XML file in Java

In this tutorial, we show how to read and parse an XML file using DOM parser provided by JDK. If you’re interested in SAX or STAX parser, please refer to these tutorials: SAX parser, STAX parser. 1- Students.xml Consider we have the following Students.xml file:

2- Instantiate XML file DOM parser loads the whole XML document into the memory and considers every XML tag...

ClassNotFoundException vs NoClassDefFoundError

ClassNotFoundException and NoClassDefFoundError occur when a required class is not found in the classpath at runtime. In this tutorial, we resolve the conflict between these 2 exceptions and explain the different scenarios of reproducing each one of them. 1- ClassNotFoundException As per java doc, this exception is thrown when trying to instantiate a class using one of these methods: forName(), findSystemClass(),...

How to iterate a Map in Java

This tutorial shows several ways for iterating a Map in Java. 1- Entry Set The common way for iterating a Map in Java is through entrySet() method as the following:

2- Iterator You can also use entrySet() along with iterator() methods to iterate a Map through an Iterator:

3- keySet() Using keySet() method, you can retrieve all the...

How to iterate a List in Java

This tutorial shows several ways for iterating a List in Java. 1- Typical For loop Using a typical For loop, you can iterate a List as the following:

2- Enhanced For loop Since JDK 5.0, you can iterate a List using an enhanced For loop which works as the following:

3- Typical While loop Using a typical while...

ArrayList vs Vector

ArrayList and Vector collections are used interchangeably for storing several items with the same type inside the memory. In this tutorial, we list the major similarities and differences between these 2 collections. 1- Similarities Both collections implement List interface, hence they expose similar operations for add, edit and delete operations. Both store their items in a dynamic Array inside the memory. Both...

Hashtable VS HashMap

Hashing objects like HashMap and Hashtable are mostly used for storing and caching data as a key/value pairs inside the memory. In this tutorial, we list the major similarities and differences between these 2 data types. 1- Similarities Both expose the same interface for adding, removing and accessing elements from the memory. get(): retrieve an item by a key. remove():...

Java – Convert File to byte[]

This tutorial shows several ways to convert a File object to a byte[] array in Java. 1- Traditional way The traditional conversion way is through using read() method of InputStream as the following:

2- Java NIO With Java 7, you can do the conversion using Files utility class of nio package:

3- Apache Commons IO Besides JDK, you can...

Java – Convert byte[] to File

This tutorial shows several ways to convert a byte[] array to File in Java. 1- Traditional way The traditional way of doing the conversion is through using FileOutputStream as the following:

2- Java NIO With Java 7, you can do the conversion using Files utility class of nio package:

3- Apache Commons IO Besides JDK, you can do...