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 (a) DOM Parser and (b) SAX parser.
Task (a): DOM Parser
File Name:
users.xml
<users-details>
<user>
<userid>1111</userid>
<name>Sammulal</name>
<address>Hyderabad</address>
<gender>Male</gender>
</user>
<user>
<userid>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>Sridhar</name>
<address>USA</address>
<gender>Male</gender>
</user>
<user>
<userid>1115</userid>
<name>Rajitha</name>
<address>Pune</address>
<gender>Female</gender>
</user>
<user>
<userid>1116</userid>
<name>Aradhya</name>
<address>Hyderabad</address>
<gender>Female</gender>
</user>
<user>
<userid>1117</userid>
<name>Vikram</name>
<address>Lundon</address>
<gender>Male</gender>
</user>
<user>
<userid>1118</userid>
<name>SaiKrishna</name>
<address>Pune</address>
<gender>Male</gender>
</user>
<user>
<userid>1119</userid>
<name>Deeksha</name>
<address>NewYork</address>
<gender>Female</gender>
</user>
<user>
<userid>1120</userid>
<name>Charan</name>
<address>Chennai</address>
<gender>Male</gender>
</user>
</users-details>
File Name: Exp5aDOMParser.java
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;
NodeList nl =
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("User id :
" +
e.getElementsByTagName("userid").item(0).getTextContent());
System.out.println("User Name :
" +
e.getElementsByTagName("name").item(0).getTextContent());
System.out.println("Address :
" +
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!!!");
}
}
Output
Compile User.java
javac
User.java
Compile Exp5aDOMParser.java
javac
Exp5aDOMParser.java
Run Exp5aDOMParser
java Exp5aDOMParser
Task (b): SAX Parser
File Name:
users.xml
<Users-details>
<User
type="male">
<Id>1111</Id>
<Name>Sammulal</Name>
<Address>Hyderabad</Address>
</User>
<User
type="female">
<Id>1112</Id>
<Name>Nakshatra</Name>
<Address>Banglore</Address>
</User>
<User
type="male">
<Id>1113</Id>
<Name>Kishor</Name>
<Address>Karimnagar</Address>
</User>
<User
type="male">
<Id>1114</Id>
<Name>Sridhar</Name>
<Address>USA</Address>
</User>
<User
type="female">
<Id>1115</Id>
<Name>Rajitha</Name>
<Address>Pune</Address>
</User>
<User
type="female">
<Id>1116</Id>
<Name>Aradhya</Name>
<Address>Hyderabad</Address>
</User>
<User
type="male">
<Id>1117</Id>
<Name>Vikram</Name>
<Address>Lundon</Address>
</User>
<User
type="male">
<Id>1118</Id>
<Name>SaiKrishna</Name>
<Address>Pune</Address>
</User>
<User
type="female">
<Id>1119</Id>
<Name>Deeksha</Name>
<Address>NewYork</Address>
</User>
<User
type="male">
<Id>1120</Id>
<Name>Charan</Name>
<Address>Chennai</Address>
</User>
</Users-details>
File Name: User.java
public class User
{
public String name;
public int id;
public String address, type;
public User() {
public User(int id, String name, String
address ,String type) {
this.name = name;
this.address = address;
this.id
= id;
this.type = type;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(getId());
sb.append(",");
sb.append(getName());
sb.append(",");
sb.append(getType());
sb.append(",");
sb.append(getAddress());
return sb.toString();
}
}
File Name:
Exp5bSAXParser.java
import java.io.IOException;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
public class Exp5bSAXParser extends DefaultHandler
{
ArrayList myUsers;
private String tempVal;
private User tempUser;
public Exp5bSAXParser(){
myUsers = new ArrayList();
}
public void runExample() {
parseDocument();
printData();
}
private void parseDocument() {
SAXParserFactory sf =
SAXParserFactory.newInstance();
try {
SAXParser sp = sf.newSAXParser();
sp.parse("users.xml", this);
} catch(SAXException se) {
se.printStackTrace();
} catch(ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException ie) {
ie. printStackTrace();
}
}
private void printData()
{
Iterator itr = myUsers.iterator();
System.out.println("No of Users :
" + myUsers.size());
Scanner in = new Scanner(System.in);
System.out.println("Enter User ID : ");
int n = in.nextInt();
int flag=0;
while(itr.hasNext())
{
User st=(User)itr.next();
If (st.id==n)
{
System.out.println("ID"+" Name"+" Type"+" Address");
System.out.println("=========================
================");
System.out.println(st.id+" "+st.name+"
"+st.type+" "+st.address);
flag=1;
break;
}
else
flag= 0;
}
if (flag==0) {
System.out.println("User ID is not
Present. Try Again!!!");
}
}
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException
{
tempVal = "";
if (qName.equalsIgnoreCase("User"))
{
tempUser = new User();
tempUser.setType(attributes.getValue("type"));
}
}
public void
characters(char[] ch, int start, int length) throws SAXException {
tempVal = new String(ch,start,length);
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
if(qName.equalsIgnoreCase("User"))
{
myUsers.add(tempUser);
} else if (qName.equalsIgnoreCase("Name"))
{
tempUser.setName(tempVal);
} else if
(qName.equalsIgnoreCase("Id")) {
tempUser.setId(Integer.parseInt(tempVal));
}else if
(qName.equalsIgnoreCase("Address")) {
tempUser.setAddress(tempVal);
}
}
public static void main(String[] args){
Exp5bSAXParser usp = new Exp5bSAXParser();
usp.runExample();
}
}
Output
Compile User.java
javac User.java
Compile Exp5bSAXParser.java
javac Exp5bSAXParser.java
Run Exp5bSAXParser
java Exp5bSAXParser
For more info regarding technology and pro stuff...visit http://peoplezmind.com/
Comments
Post a Comment