AddressBookLoader

AddressBookLoader - S

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.io.File; i import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.*; import org.xml.sax.helpers.*; i public class AddressBookLoader extends DefaultHandler{ p private AddressBook ad; private boolean tagAddressBook; p public AddressBookLoader(){ ad = new AddressBook(); tagAddressBook = false; } public AddressBook getAddressBook(){ return ad; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { A if(qName.equalsIgnoreCase("AddressBook")){ tagAddressBook = true; } BuddyInfo bi = new BuddyInfo(attributes.getValue("name"), attributes.getValue("address"), attributes.getValue("phone"), Integer.parseInt(attributes.getValue("age"))); ad.addBuddy(bi); } } @Override public void endElement (String uri, String localName, String qName)throws SAXException{
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: S if(qName.equalsIgnoreCase("AddressBook")){ tagAddressBook = false; } } @Override public void characters(char ch, int start, int length){ } // static method for DOM public static AddressBook readDOM(File f) throws Exception{ p AddressBook aBook = new AddressBook(); Element el = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(f).getDocumentEleme nt(); n if(el.getNodeName().equals("AddressBook")){ NodeList nl = el.getChildNodes(); BuddyInfo bi; for(int i = 0; i < nl.getLength();i++){ if(nl.item(i).getNodeName().equals("BuddyInfo")){ bi = new BuddyInfo( ((Element)nl.item(i)).getAttribute("name"), ((Element)nl.item(i)).getAttribute("address"), ((Element)nl.item(i)).getAttribute("phone"), Integer.parseInt(((Element)nl.item(i)).getAttribute("age"))); aBook.addBuddy(bi); } } } return aBook; } }...
View Full Document

Page1 / 2

AddressBookLoader - S

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online