Read XML file in Java using dom xml and sax xml parser

Wednesday, December 30th, 2015 (0 Comment)

In this article, I’ll guide how to read XML file via the DOM XML Parser and SAX XML Parser.

Note: DOM Parser is slow and memory-consuming when it downloads an XML document contains too much data. SAX Parser will use faster and less memory than DOM Parser.

1. Read xml file by dom parser

DOM (Document Object Model): its mechanism is to read the entire contents of XML files in memory (so if large XML is slow and can program memory wasted because not always do also want to read the XML content).

Create xml file with name is “room.xml”

import libraries necessary

Main Program

Run Output

2. Read xml file by sax parser

SAX parser is different than DOMParser. It does not load the XML document into memory and it also does not generate any object in the XML document representation. SAX uses callback functions Function.

Create Room class to store information read from xml file

Create SAXSample class extended from DefaultHandler to handle read xml file

Main Program

Output:

 

Tags: , , ,

Related Posts

core java
Export Crystal Report with Subreport in java
core java
Convert xml to object java and vice versa
core java
Java basic tutorial on lvtutorial.com
core java
Java hibernate manytomany to manage user role

Related Posts

core java
Export Crystal Report with Subreport in java
core java
Convert xml to object java and vice versa
core java
Java basic tutorial on lvtutorial.com
core java
Java hibernate manytomany to manage user role

Leave a Reply

Your email address will not be published. Required fields are marked *