Menu
					
											
				
				
			- 14. A web application for implementation: The user is first served a login page which takes user's name and password. After Submitting the details the server checks these values against the data from a database and takes the following decisions.
- • If name and password matches, serves a welcome page with user's full name.
- • If name matches and password doesn't match, then serves “password mismatch” page
- • If name is not found in the database, serves a registration page, where user’s full name is asked and on submitting the full name, it stores, the login name, password and full name in the database (hint: use session for storing the submitted login name and password)
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
