Aim: Create an XML document that contains 10 users information. Write a Java program, which takes User Id as input and returns the user details by taking the user information from the XML document using DOM parser

XML File (employees.xml):


<employees>
    <employee id="111">
        <firstName>Shiva</firstName>
        <lastName>Kumar</lastName>
        <location>India</location>
    </employee>
    <employee id="222">
        <firstName>Kumar</firstName>
        <lastName>Gussin</lastName>
        <location>Russia</location>
    </employee>
    <employee id="333">
        <firstName>David</firstName>
        <lastName>Feezor</lastName>
        <location>USA</location>
    </employee>
    <employee id="444">
        <firstName>cmrTpoint</firstName>
        <lastName>.com</lastName>
        <location>Worldwide</location>
    </employee>
    <employee id="555">
        <firstName>Swathi</firstName>
        <lastName>Gaibe</lastName>
        <location>India</location>
    </employee>
    <employee id="666">
        <firstName>Uday</firstName>
        <lastName>Maharaj</lastName>
        <location>Russia</location>
    </employee>
    <employee id="777">
        <firstName>Veeru</firstName>
        <lastName>Feezor</lastName>
        <location>USA</location>
    </employee>
    <employee id="888">
        <firstName>Pavan</firstName>
        <lastName>Feezor</lastName>
        <location>USA</location>
    </employee>
    <employee id="999">
        <firstName>Narayana</firstName>
        <lastName>Gussin</lastName>
        <location>Russia</location>
    </employee>
    <employee id="1000">
        <firstName>Sunder</firstName>
        <lastName>Feezor</lastName>
        <location>USA</location>
    </employee>
</employees>
        
Java Program (ReadXML.java):


import org.w3c.dom.*; 
import javax.xml.parsers.*; 
import java.io.*;
import java.util.Scanner;

public class ReadXML {
    public static void main(String a[]) throws Exception { 
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        // Load XML file
        Document document = builder.parse(new File("employees.xml"));
        document.getDocumentElement().normalize();

        // Root node
        Element root = document.getDocumentElement();

        // Get all employees
        NodeList nList = document.getElementsByTagName("employee"); 
        System.out.println("Enter employee id:");
        Scanner s = new Scanner(System.in);
        String id = s.next();

        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node node = nList.item(temp);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) node;
                if (eElement.getAttribute("id").equals(id)) { 
                    System.out.println("First Name : " + eElement.getElementsByTagName("firstName").item(0).getTextContent());
                    System.out.println("Last Name : " + eElement.getElementsByTagName("lastName").item(0).getTextContent());
                    System.out.println("Location : " + eElement.getElementsByTagName("location").item(0).getTextContent());
                }
            }
        }
    }
}
        
Output:

Enter employee id: 555
First Name : Swathi
Last Name : Gaibe
Location : India

;