source: other-projects/tipple-android/src/org/tipple/mapDisplay/XMLToHashmapHandler.java@ 26523

Last change on this file since 26523 was 26523, checked in by davidb, 11 years ago

A version of Tipple with the mods Casey made for her Masters work

File size: 1.9 KB
Line 
1package org.tipple.mapDisplay;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5
6import org.xml.sax.Attributes;
7import org.xml.sax.SAXException;
8import org.xml.sax.helpers.DefaultHandler;
9
10public class XMLToHashmapHandler extends DefaultHandler {
11 String lockon_element_;
12 Boolean locked_on_;
13 ArrayList<HashMap<String, String>> lockon_list_;
14
15 HashMap<String, String> hashmap_;
16 String active_element_;
17 StringBuffer inner_text_;
18
19 public XMLToHashmapHandler(String lockon_element) {
20 // initial settings to parse a file
21 lockon_element_ = lockon_element;
22 lockon_list_ = new ArrayList<HashMap<String, String>>();
23 locked_on_ = false;
24 }
25
26 public ArrayList<HashMap<String, String>> getHashmapList() {
27 return lockon_list_;
28 }
29
30 @Override
31 public void startElement(String uri, String localName, String qName,
32 Attributes attributes) throws SAXException {
33 if (localName.equalsIgnoreCase(lockon_element_)) {
34 hashmap_ = new HashMap<String, String>();
35 locked_on_ = true; // => covert each child element inside this to a
36 // hashmap key
37 } else if (locked_on_) {
38 active_element_ = localName.toLowerCase();
39 inner_text_ = new StringBuffer();
40 }
41 // else ignore element as outside area of interest
42 }
43
44 @Override
45 public void endElement(String uri, String localName, String qName)
46 throws SAXException {
47 if (localName.equalsIgnoreCase(lockon_element_)) {
48 // push hashmap onto arraylist
49 lockon_list_.add(hashmap_);
50 hashmap_ = null;
51
52 locked_on_ = false;
53 } else if (localName.equalsIgnoreCase(active_element_)) {
54
55 hashmap_.put(active_element_, inner_text_.toString());
56 active_element_ = null;
57 inner_text_ = null;
58 }
59 }
60
61 @Override
62 public void characters(char[] ch, int start, int length)
63 throws SAXException {
64 if ((locked_on_) && (active_element_ != null)) {
65 String text = new String(ch, start, length);
66 inner_text_.append(text);
67 }
68 }
69
70}
Note: See TracBrowser for help on using the repository browser.