Skip to main content

DOM and SAX parser-Week5

 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

Popular posts from this blog

Bit Stuffing Program

#include<stdio.h> main() { int i,j,k,n,count=0; int a[30],b[30]; printf("The flag is 01111110"); printf("\n Enter the message size:"); scanf("%d",&n); printf("\n Enter the message in bits(0/1):"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\n The message is:"); for(i=0;i<n;i++) { printf("%d",a[i]); } printf("\n"); for(i=0,j=0;i<n;i++,j++) { if(a[i]==1) { if(count==5) { b[j]=0; j++; count=0; } else count++; b[j]=a[i]; } k=j; printf("\n The coded message is:"); printf("01111110 /t"); for(j=0;j<k;j++) { printf("%d",b[j]); } printf("\01111110\n"); count=0; printf("The encoded message is :\n"); printf("\n 01111110 \t"); for(i=0;i<k;i++) { if(b[i]==1 && count<5) { printf("%d",b[i]...

CPU Schedueling programs

        Fcfs program         #include<stdio.h> #include<conio.h> main() { int bt[20], wt[20], tat[20], i, n; float wtavg, tatavg; printf("\nEnter the number of processes -- "); scanf("%d", &n); for(i=0;i<n;i++) { printf("\nEnter Burst Time for Process %d -- ", i); scanf("%d", &bt[i]); } wt[0] = wtavg = 0; tat[0] = tatavg = bt[0]; for(i=1;i<n;i++) { wt[i] = wt[i-1] +bt[i-1]; tat[i] = tat[i-1] +bt[i]; wtavg = wtavg + wt[i]; tatavg = tatavg + tat[i]; } printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n"); for(i=0;i<n;i++) printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]); printf("\nAverage Waiting Time -- %f", wtavg/n); printf("\nAverage Turnaround Time -- %f", tatavg/n); getch(); } Output SJF program #include<stdio.h> #include<conio.h> main() { int p[20], bt[20], wt[20],...