source: other-projects/tipple-android/tipple-lib/src/org/greenstone/android/tipple/base/XMLToHashmapHandler.java@ 26899

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

Tipple reborn after Chris's Summer of Code 2013

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