0% found this document useful (0 votes)
58 views4 pages

WT 7th

The document describes a Java program that takes a user ID as input and returns user details by parsing an XML document containing information for 10 users. The program uses DOM parsing to load the XML file, find the matching user by ID, and output the user's name, address, and gender. It notifies the user if the ID is not found in the XML document.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views4 pages

WT 7th

The document describes a Java program that takes a user ID as input and returns user details by parsing an XML document containing information for 10 users. The program uses DOM parsing to load the XML file, find the matching user by ID, and output the user's name, address, and gender. It notifies the user if the ID is not found in the XML document.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

MARRI LAXMAN REDDY INSTITUTE OF TECHNOLOGY & MANAGEMENT

Exp. No.07
Date:

AIM: Create and save an XML document on the server, which contains 10 users information.
Write a program, which takes User Id as an input and returns the user details by taking the

user information from the XML document. CODE:

import java.io.File;
import javax.xml.parsers.*;

import org.w3c.dom.*;
import java.util.Scanner;

public class Exp5aDOMParser{


public static void main(String[] args) throws Exception {

DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();


DocumentBuilder b = fac.newDocumentBuilder();

Document doc = b.parse(new File("users.xml"));


doc.getDocumentElement().normalize();

Element root = doc.getDocumentElement();


Scanner in = new Scanner(System.in);

System.out.println("Enter User
ID:"); int n = in.nextInt(); int flag =
0;
NodeListnl = doc.getElementsByTagName("user");
for(int i = 0; i<nl.getLength(); i++)
{

Node node = nl.item(i);


if(node.getNodeType() ==
Node.ELEMENT_NODE)

{
Element e = (Element) node; int x =
Integer.parseInt(e.getElementsByTagName("userid").item(0).getTextContent());
if(x==n)

{
System.out.println(root.getNodeName());

System.out.println("============================================");System.out.println("Us
er id :" +

Regd. No.197Y1A0559 Page No.


MARRI LAXMAN REDDY INSTITUTE OF TECHNOLOGY & MANAGEMENT
Exp. No.07
Date:

e.getElementsByTagName("userid").item(0).getTextContent());System.out.println("User Name :" +

e.getElementsByTagName("name").item(0).getTextContent());

System.out.println("Adress :" +

e.getElementsByTagName("address").item(0).getTextContent());

System.out.println("Gender :" +

e.getElementsByTagName("Gender").item(0).getTextContent());
flag=1;

break;

else

flag=0;

}
}

}
if(flag==0)

System.out.println("User Id is not present.Try Again!!!");


}

users.xml:

<users-details>
<user>

<userid>1111</userid>
<name>Sammulal</name>

<address>Hyderabad</address>
<gender>Male</gender>
< /user>

Regd. No.197Y1A0559 Page No.


MARRI LAXMAN REDDY INSTITUTE OF TECHNOLOGY & MANAGEMENT
Exp. No.07
Date:

<<useruserid>1112</userid> >

<name>Sanjana</name>

<address>Banglore</address>

<gender>Female</gender>

</user>
<user>
<userid>1113</userid>

<name>kishor</name>
<address>karimnagar</address>

<gender>Male</gender>
</user>

<user>

<userid>1114</userid>
<name>Srindhar</name>

<address>USA</address>
<gender>Male</gender>

</user>

<user>

<userid>1115</userid>

<name>Rajitha</name>

<address>Hyderbad</address>

<gender>Female</gender>

</user>
<user>

<userid>1116</userid>

<name>Aradhya</name>

<address>Hyderabad</address>

<gender>Female</gender>

Regd. No.197Y1A0559 Page No.


MARRI LAXMAN REDDY INSTITUTE OF TECHNOLOGY & MANAGEMENT
Exp. No.07
Date:

</user>

<user>

<userid>1117</userid>

<name>Vikram</name>

<address>Ludan</address>

<gender>Male</gender>

</user>

<user>

<userid>1118</userid>

<name>SaiKrishna</name>

<address>Pune</address>

<gender>Male</gender>

</user>

OUTPUT:

Regd. No.197Y1A0559 Page No.

You might also like