Question : Help me understand this class so that I can modify it?

Hi...  :)

I am following this blog post and try to update the class to get different data from the XML file...
   http://www.smnirven.com/?p=39

This is the actual call to the Geocoder class from the ReverseGeocodeLookupTask class...
1:
localityName = Geocoder.reverseGeocode(currentLocation);

The Geocoder class then calls Google Maps API to get the XML back...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
InputStreamReader isr =  new InputStreamReader(connection.getInputStream());
InputSource source = new InputSource(isr);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xr = parser.getXMLReader();
GoogleReverseGeocodeXmlHandler handler = new GoogleReverseGeocodeXmlHandler();

xr.setContentHandler(handler);
xr.parse(source);

localityName = handler.getLocalityName();

I see that it is calling GoogleReverseGeocodeXmlHandler class, which is shown below, but I don't really understand how this class works...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
public class GoogleReverseGeocodeXmlHandler extends DefaultHandler
{
    private boolean inLocalityName = false;
    private boolean finished = false;
    private StringBuilder builder;
    private String localityName;
   
    public String getLocalityName()
    {
        return this.localityName;
    }
   
    @Override
    public void characters(char[] ch, int start, int length)
           throws SAXException {
        super.characters(ch, start, length);
        if (this.inLocalityName && !this.finished)
        {
            if ((ch[start] != '\n') && (ch[start] != ' '))
            {
                builder.append(ch, start, length);
            }
        }
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException
    {
        super.endElement(uri, localName, name);
       
        if (!this.finished)
        {
            if (localName.equalsIgnoreCase("LocalityName"))
            {
                this.localityName = builder.toString();
                this.finished = true;
            }
           
            if (builder != null)
            {
                builder.setLength(0);
            }
        }
    }

    @Override
    public void startDocument() throws SAXException
    {
        super.startDocument();
        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("LocalityName"))
        {
            this.inLocalityName = true;
        }
    }
}

The XML output looks like this...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
<?xml version="1.0"?>
<AddressDetails xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" Accuracy="8">
	<Country>
		<CountryNameCode>US</CountryNameCode>
		<CountryName>USA</CountryName>
		<AdministrativeArea>
			<AdministrativeAreaName>CA</AdministrativeAreaName>
			<SubAdministrativeArea>
				<SubAdministrativeAreaName>Santa Clara</SubAdministrativeAreaName>
				<Locality>
					<LocalityName>Mountain View</LocalityName>
					<Thoroughfare>
						<ThoroughfareName>1600 Amphitheatre Pkwy</ThoroughfareName>
					</Thoroughfare>
					<PostalCode>
						<PostalCodeNumber>94043</PostalCodeNumber>
					</PostalCode>
				</Locality>
			</SubAdministrativeArea>
		</AdministrativeArea>
	</Country>
</AddressDetails>

What I'd like to do is to get both localityName and countryNameCode at the same time...  
What I am not able to find is 'what' variable contains the whole XML string and how this GoogleReverseGeocodeXmlHandler class works since I don't see anything like a constructor and whatnot...

Thank you very much...  :)

CyanBlue

Answer : Help me understand this class so that I can modify it?

All that is absolutely standard SAX parsing. It's easy to read up on it:

http://www.cafeconleche.org/books/xmljava/chapters/ch06.html
Random Solutions  
 
programming4us programming4us