adsa -...

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

View Full Document Right Arrow Icon
public String toXML(){ String s = "\t<BuddyInfo "; s += "name=\"" + this.name + "\" "; s += "address=\"" + this.address + "\" "; s += "phone=\"" + this.phoneNumber + "\" "; s += "age=\"" + this.age + "\">"; return s + "</BuddyInfo>\n"; } public String toXML(){ String s = "<AddressBook>\n"; for(BuddyInfo bi : buddies){ s += bi.toXML(); } return s + "</AddressBook>"; } public void writeXML(File f){ try { FileWriter fWriter = new FileWriter(f); fWriter.write(this.toXML()); fWriter.close(); } catch (IOException e) { e.printStackTrace(); } } // static method for DOM public static AddressBook readDOM(File f) throws Exception{ p AddressBook aBook = new AddressBook(); Element el = DocumentBuilderFactory.newInstance() .newDocumentBuilder().parse(f).getDocumentElement(); . 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"),
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: ((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; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { A if(qName.equalsIgnoreCase("AddressBook")){ tagAddressBook = true; } if(tagAddressBook && qName.equalsIgnoreCase("BuddyInfo")){ 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{ S if(qName.equalsIgnoreCase("AddressBook")){ tagAddressBook = false; } } @Override public void characters(char ch, int start, int length){ }...
View Full Document

This note was uploaded on 04/13/2010 for the course SYSC 2101 taught by Professor Babac during the Winter '10 term at Carleton CA.

Page1 / 2

adsa -...

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