Download java program to read xml file - will
How to Read XML File in Java
Reading XML file in Java is much different from reading other files like .docx and .txt because XML file contains data between the tags. Java provides many ways to parse an XML file. There are two parsers in Java which parses an XML file:
- Java DOM Parser
- Java SAX Parser
Java DOM Parser
The DOM API provides the classes to read and write an XML file. We can create, delete, modify, and rearrange the node using the DOM API. DOM parser parses the entire XML file and creates a DOM object in the memory. It models an XML file in a tree structure for easy traversal and manipulation. In DOM everything in an XML file is a node. The node represents a component of an XML file. DOM parser is slow in process and occupies lots of memory when it loads an XML file into memory.
We must have followed the process to read an XML file in Java:
- Instantiate XML file: DOM parser loads the XML file into memory and consider every tag as an element.
- Get root node: Document class provides the getDocumentElement() method to get the root node and the element of the XML file.
- Get all nodes: The getElementByTagName() method retrieves all the specific tag name from the XML file. Where ELEMENT_NODE type refers to a non-text node which has sub-elements. If we need to access all nodes from the starting, including the root node, we can recursively call the getChildElement() method.
- Get Node by text value: We can use getElementByTextValue() method in order to search for a node by its value.
- Get Node by attribute value: If we want to search a node by the value of a specific attribute, we can use the getElementByTagName() method along with getAttribute() method.
Steps to Read XML File in Java Using eclipse
Step 1: Create a simple Java project.
Step 2: Create a class file and provide a class file name. We have created the class file with the name ReadXMLFileExample1.
Step 3: Write the following code.
Step 4: Download domjaxbjar file: Click here
Step 5: Create a lib folder in the project.
Step 6: Copy domjaxbjar file and paste into the lib folder.
Step 7: Set the class path:
Right-click on the project->Build Path->Configure Build Path->Add External JARs->Select the JAR file->click on Open button->Apply and Close.
Step 8: Create an XML file. We have created an XML file with name diseinuak4web.net and write the following data into it.
Step 9: Run the project.
Creating XML file: diseinuak4web.net
Example of reading XML file using DOM Parser
Output:
Let's see another example of reading xml file.
Example of reading XML file using DOM Parser
The following example reads the same XML file diseinuak4web.net, and showing that how to loop the node one by one. It prints the node value, name and attribute if any.
Example
Output:
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
Output:
-