Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.google.android.iopex;
- import java.util.ArrayList;
- import java.util.List;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- import static com.google.android.iopex.BaseXMLParser.*;
- public class SaxXMLHandler extends DefaultHandler{
- private List<DbEntry> entries;
- private DbEntry currentEntry;
- private StringBuilder builder;
- public List<DbEntry> getMessages(){
- return this.entries;
- }
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- super.characters(ch, start, length);
- builder.append(ch, start, length);
- }
- @Override
- public void endElement(String uri, String localName, String name)
- throws SAXException {
- super.endElement(uri, localName, name);
- if (this.currentEntry != null){
- String escapedString = builder.toString();
- if (localName.equalsIgnoreCase(EMP_ID)){
- String str = escapedString.trim();
- currentEntry.setEmployeeID(str);
- } else if (localName.equalsIgnoreCase(EMP_NAME)){
- currentEntry.setEmployeeName(escapedString);
- } else if (localName.equalsIgnoreCase(DESIGNATION)){
- currentEntry.setDesignation(escapedString);
- } else if (localName.equalsIgnoreCase(DEPARTMENT)){
- currentEntry.setDepartment(escapedString);
- } else if (localName.equalsIgnoreCase(EMP_PHOTO)){
- currentEntry.setImageForEmployee(escapedString);
- } else if (localName.equalsIgnoreCase(ELIGIBLE_FOR)){
- currentEntry.setEligibleFor(escapedString);
- }
- else if (localName.equalsIgnoreCase(ROOT_EMP_DET)){
- entries.add(currentEntry);
- currentEntry = null;
- }
- builder.setLength(0);
- }
- }
- @Override
- public void startDocument() throws SAXException {
- super.startDocument();
- entries = new ArrayList<DbEntry>();
- builder = new StringBuilder();
- }
- @Override
- public void startElement(String uri, String localName, String name,
- Attributes attributes) throws SAXException {
- super.startElement(uri, localName, name, attributes);
- if (localName.equalsIgnoreCase(ROOT_EMP_DET)){
- this.currentEntry = new DbEntry();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement