https://www.javatpoint.com/how-to-read-xml-file-in-java Java SAX Parser Java SAX parser stands for Simple API for XML. SAX parser parses an XML file line by line. It triggers events when it encounters the opening tag, closing tag, and character data in an xml file. SAX parser is also called the event-based parser. SAX parser does not load any XML file into memory. It does not create any object representation of the XML document. SAX parser uses call back function to inform clients of the XML document structure. It is faster and uses less memory than DOM parser. SAX is a streaming interface for XML, which means that XML file parses in sequential order starting at the top of the document, and ending with the closing of the root element. Example of reading XML file using SAX parser 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; public class ReadXMLFileExample3 { public static void main(String args[]) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean id = false; boolean firstname = false; boolean lastname = false; boolean subject = false; boolean marks = false; //parser starts parsing a specific element inside the document public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println("Start Element :" + qName); if(qName.equalsIgnoreCase("Id")) { id=true; } if (qName.equalsIgnoreCase("FIRSTNAME")) { firstname = true; } if (qName.equalsIgnoreCase("LASTNAME")) { lastname = true; } if (qName.equalsIgnoreCase("SUBJECT")) { subject = true; } if (qName.equalsIgnoreCase("MARKS")) { marks = true; } } //parser ends parsing the specific element inside the document public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("End Element:" + qName); } //reads the text value of the currently parsed element public void characters(char ch[], int start, int length) throws SAXException { if (id) { System.out.println("ID : " + new String(ch, start, length)); id = false; } if (firstname) { System.out.println("First Name: " + new String(ch, start, length)); firstname = false; } if (lastname) { System.out.println("Last Name: " + new String(ch, start, length)); lastname = false; } if (subject) { System.out.println("Subject: " + new String(ch, start, length)); subject = false; } if (marks) { System.out.println("Marks : " + new String(ch, start, length)); marks = false; } } }; saxParser.parse("F:\\XMLFile.xml", handler); } catch (Exception e) { e.printStackTrace(); } } } Output: Start Element: class Start Element: student Start Element: id ID: 101 End Element: id Start Element: firstname First Name: Naman End Element: firstname Start Element: lastname Last Name: Kumar End Element: lastname Start Element: subject Subject: Math End Element: subject Start Element: marks Marks: 83 End Element: marks End Element: student Start Element: student Start Element: id ID: 102 End Element: id Start Element: firstname First Name: Kapil End Element: firstname Start Element: lastname Last Name: Kumar End Element: lastname Start Element: subject Subject: Chemistry End Element: subject Start Element: marks Marks: 60 End Element: marks End Element: student Start Element: student Start Element: id ID: 103 End Element: id Start Element: firstname First Name: Harsh End Element: firstname Start Element: lastname Last Name: Singh End Element: lastname Start Element: subject Subject: English End Element: subject Start Element: marks Marks: 70 End Element: marks End Element: student Start Element: student Start Element: id ID: 104 End Element: id Start Element: firstname First Name: Jitesh End Element: firstname Start Element: lastname Last Name: Singh End Element: lastname Start Element: subject Subject: Physics End Element: subject Start Element: marks Marks: 76 End Element: marks End Element: student End Element: class