Search This Blog

Sunday 28 July 2013

Converting XML into java objects by using SAX Parser



Converting XML into java objects by using SAX Parser


src\AccountXmlHandler.java
==========================================

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author Pedababu M
 *
 */
public class AccountXmlHandler extends DefaultHandler {

       private Account acct;
       private String temp;
       private ArrayList<Account> accList = new ArrayList<Account>();


       public static void main(String[] args) throws IOException, SAXException,
                     ParserConfigurationException {
              SAXParserFactory spfac = SAXParserFactory.newInstance();
              SAXParser sp = spfac.newSAXParser();
              AccountXmlHandler handler = new AccountXmlHandler();
              sp.parse("D:\\workspace\\XmlToBean\\src\\bank.xml", handler);
            
              handler.readList();

       }



       public void characters(char[] buffer, int start, int length) {
              temp = new String(buffer, start, length);
       }
     

       public void startElement(String uri, String localName,
                     String qName, Attributes attributes) throws SAXException {
           System.out.println("startElement --- uri" + uri);
           System.out.println("startElement --- qName::::" + qName);
           System.out.println("startElement --- localName" + localName);
           System.out.println("startElement --- attributes" + attributes.getLocalName(1));
              temp = "";
              if (qName.equalsIgnoreCase("Account")) {
                     acct = new Account();
                     acct.setType(attributes.getValue("type"));

              }
       }


       public void endElement(String uri, String localName, String qName)
                     throws SAXException {

              if (qName.equalsIgnoreCase("Account")) {
                     accList.add(acct);

              } else if (qName.equalsIgnoreCase("name")) {
                     acct.setName(temp);
              } else if (qName.equalsIgnoreCase("accountid")) {
                     acct.setAccountId(Integer.parseInt(temp));
              } else if (qName.equalsIgnoreCase("blanceamount")) {
                     acct.setBlanceAmount(Integer.parseInt(temp));
              } else if (qName.equalsIgnoreCase("ssn")) {
                     acct.setSsn(temp);
              }

       }

       private void readList() {
              System.out.println("No of  the accounts in bank '" + accList.size()  + "'.");
              Iterator<Account> it = accList.iterator();
              while (it.hasNext()) {
                     System.out.println(it.next().toString());
              }
       }
     
}

src\Account.java
================================

/**
 * @author Pedababu M
 *
 */
public class Account {
          private String name;
           private int accountId;
            private int blanceAmount;
           private String type;
           String ssn;

           public Account() {
           }

           public Account(String name, int accountId, int blanceAmount, String type) {
                  this.name = name;
                  this.blanceAmount = blanceAmount;
                  this.accountId = accountId;
                  this.type = type;
           }

           /**
             * @return the name
             */
            public String getName() {
                return name;
            }

            /**
             * @param name the name to set
             */
            public void setName(String name) {
                this.name = name;
            }

            /**
             * @return the accountId
             */
            public int getAccountId() {
                return accountId;
            }

            /**
             * @param accountId the accountId to set
             */
            public void setAccountId(int accountId) {
                this.accountId = accountId;
            }

            /**
             * @return the blanceAmount
             */
            public int getBlanceAmount() {
                return blanceAmount;
            }

            /**
             * @param blanceAmount the blanceAmount to set
             */
            public void setBlanceAmount(int blanceAmount) {
                this.blanceAmount = blanceAmount;
            }

            /**
             * @return the type
             */
            public String getType() {
                return type;
            }

            /**
             * @param type the type to set
             */
            public void setType(String type) {
                this.type = type;
            }

            /**
             * @return the ssn
             */
            public String getSsn() {
                return ssn;
            }

            /**
             * @param ssn the ssn to set
             */
            public void setSsn(String ssn) {
                this.ssn = ssn;
            }

           public String toString() {
                  StringBuffer sb = new StringBuffer();
                  sb.append("Account Details - ");
                  sb.append("Name:" + getName());
                  sb.append(", ");
                  sb.append("Account Type:" + getType());
                  sb.append(", ");
                  sb.append("Account Id:" + getAccountId());
                  sb.append(", ");
                  sb.append("Balance amount:" + getBlanceAmount());
                  sb.append(",");
                  sb.append("Ssn:" + getSsn());
                  sb.append(".");
                  return sb.toString();
           }
}

src\bank.xml
=================================

<?xml version="1.0" encoding="UTF-8"?>
<Bank>
      <Account type="saving">
            <accountid>1001</accountid>
            <name>MPB</name>
            <blanceamount>10000</blanceamount>
            <ssn>32323-32323</ssn>
      </Account>
      <Account type="current">
            <accountid>1001</accountid>
            <name>XYZ</name>
            <blanceamount>10000</blanceamount>
            <ssn>4343434-43434</ssn>
      </Account>
</Bank>

2 comments:

  1. Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks. 数据分析代写

    ReplyDelete
  2. .留学生网课代上代考,作业论文代写,海外课程辅导平台 -你的作业代写专家assignment-daixie.com
    留学生网课代上代考,作业论文代写,海外课程辅导平台 -你的作业代写专家assignment-daixie

    ReplyDelete