Full Stack Web Development Internship Program
- 5k Enrolled Learners
- Weekend/Weekday
- Live Class
XML, eXtensible Markup Language is a markup language that defines a set of rules for encoding documents in a format that is readable. XML Parsing refers to going through an XML document in order to access or modify data. An XML Parser provides the required functionality to access or modify data in an XML document. In this article, let’s explore Java XML Parser in detail.
The XML parser is a software library or a package that provides an interface for client applications to work with XML documents. It checks for proper format of the XML document and also validates the XML documents.
Following diagram shows how XML parser interacts with XML documents:
You can think of validation as another stage beyond parsing. As the component parts of the program are identified when parsing, a validating parser can compare them with the pattern laid down by the DTD or Schema, to check that they conform.
The fundamental component of XML development is XML parsing. XML parsing for Java is a standalone XML component that parses an XML document (and at times also a standalone DTD or XML Schema) so that user program can process it. The figure below shows an XML document as input to the XML Parser for Java.
The XML Parser for Java might also include an integrated XSL Transformation (XSLT) Processor for transforming XML data using XSL stylesheets. Using the XSLT Processor, you can easily transform XML documents from XML to XML, XML to HTML, or virtually to any other text-based format.
Java provides a lot of options to parse XML documents. Some of the commonly used java XML parsers are:
In this article, let’s check out how to parse an XML File using the DOM Parser.
DOM stands for Document Object Model. DOM Parser is the easiest java XML parser to implement and learn. It parses an entire XML document, loads it into memory and constructs a tree representation of the document. Let’s consider a simple example to understand the concept.
For demo purpose, we will be using a file named Employees.xml. In the file, each employee has a unique ID, first and last name, age, and salary and the employees are separated by their IDs.
<?xml version="1.0" encoding="UTF-8"?> <Employees> <Employee ID="1"> <Firstname>Dean</Firstname > <Lastname>Winchester</Lastname> <Age>30</Age> <Salary>2500</Salary> </Employee> <Employee ID="2"> <Firstname>Sam</Firstname> <Lastname>Davis</Lastname> <Age>22</Age> <Salary>1500</Salary> </Employee> <Employee ID="3"> <Firstname>Jake</Firstname> <Lastname>Peralta</Lastname> <Age>24</Age> <Salary>2000</Salary> </Employee> <Employee ID="4"> <Firstname>Amy</Firstname> <Lastname>Griffin</Lastname> <Age>25</Age> <Salary>2250</Salary> </Employee> </Employees>
Also, in order to capture the notion of an employee, we create its respective Java class, called Employee.java as shown below:
package MyPackage; public class Employee { private String ID; private String Firstname; private String Lastname; private int age; private double salary; public Employee(String ID, String Firstname, String Lastname, int age, double salary) { this.ID = ID; this.Firstname = Firstname; this.Lastname = Lastname; this.age = age; this.salary = salary; } @Override public String toString() { return "<" + ID + ", " + Firstname + ", " + Lastname + ", " + age + ", " + salary + ">"; } }
The Document Object Model(DOM) provides APIs that let you create, modify, delete, and rearrange nodes as needed. The DOM parser parses the entire XML document and loads the XML content into a Tree structure. Using the Node and NodeList classes, we can retrieve and modify the contents of an XML file.
A sample example program in Java that loads the content of an XML file and prints its contents is shown below:
package MyPackage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class DomParserExample { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { //Get Document Builder DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // Load the input XML document, parse it and return an instance of the // Document class. Document document = builder.parse(new File("Employees.xml")); List<Employee> employees = new ArrayList<Employee>(); NodeList nodeList = document.getDocumentElement().getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element elem = (Element) node; // Get the value of the ID attribute. String ID = node.getAttributes().getNamedItem("ID").getNodeValue(); // Get the value of all sub-elements. String firstname = elem.getElementsByTagName("Firstname") .item(0).getChildNodes().item(0).getNodeValue(); String lastname = elem.getElementsByTagName("Lastname").item(0) .getChildNodes().item(0).getNodeValue(); Integer age = Integer.parseInt(elem.getElementsByTagName("Age") .item(0).getChildNodes().item(0).getNodeValue()); Double salary = Double.parseDouble(elem.getElementsByTagName("Salary") .item(0).getChildNodes().item(0).getNodeValue()); employees.add(new Employee(ID, firstname, lastname, age, salary)); } } // Print all employees. for (Employee empl: employees) System.out.println(empl.toString()); } }
The execution of code happens as follows:
Output
<1, Dean, Winchester, 30, 2500.0> <2, Sam, Davis, 22, 1500.0> <3, Jake, Peralta, 24, 2000.0> <4, Amy, Griffin, 25, 2250.0>
That’s how you can parse an XML file using the DOM Parser. DOM parser has its advantages and disadvantages.
Advantages
Disadvantages
This brings us to the end of this ‘Java XML Parser’ article.
Make sure you practice as much as possible and revert your experience.
Check out the Java Course Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.
Got a question for us? Please mention it in the comments section of this ‘Java XML Parser’ article and we will get back to you as soon as possible.
Course Name | Date | |
---|---|---|
Java Certification Training Course | Class Starts on 28th January,2023 28th January SAT&SUN (Weekend Batch) | View Details |
Java Certification Training Course | Class Starts on 25th February,2023 25th February SAT&SUN (Weekend Batch) | View Details |
edureka.co