Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

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

Avatar of CyanBlue
CyanBlueFlag for United States of America asked on
Java
4 Comments1 Solution492 ViewsLast Modified:
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...
localityName = Geocoder.reverseGeocode(currentLocation);
The Geocoder class then calls Google Maps API to get the XML back...
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...
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...
<?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
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answers