2

I am working with one application in which SAXparsing is placed. To get the City & State name from latitude and longitude I'm using Google API. Google API url google api

I want to get long_name short_name & type of header Tag address_component .

All the information I am getting successfully from this XML but problem is that when I am trying to get type Tag value . There are Two type tag in this header and I am always getting second type tag value .

Sample XML:

<address_component>
<long_name>Gujarat</long_name>
<short_name>Gujarat</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>

How can I get type Tag value is administrative_area_level_1 as well as political?

Kai
  • 38,985
  • 14
  • 88
  • 103
Hitarth
  • 1,950
  • 3
  • 27
  • 52
  • I think you should check this: [Android - SAX Parsing Example](http://www.technotalkative.com/android-sax-parsing-example/) – Paresh Mayani Dec 23 '11 at 05:24

3 Answers3

1

I came across the following link which is really easy to give a start- http://javarevisited.blogspot.com/2011/12/parse-read-xml-file-java-sax-parser.html

AKIWEB
  • 19,008
  • 67
  • 180
  • 294
0

I add the data into one file named as location.xml(if you get this from web do your own logic for getting data after getting that data convert into Inputstream pass it to following code) i wrote a method in that you can get it

public void ReadAndWriteXMLFileUsingSAXParser(){

        try
        {
            DefaultHandler handler = new MyHandler();
//              parseXmlFile("infilename.xml", handler, true);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            InputStream rStream = null;
            rStream = getClass().getResourceAsStream("location.xml");
            saxParser.parse(rStream, handler);
        }catch (Exception e) 
        {
            System.out.println(e.getMessage());
        }

    }

This is MyHandler class. your final data stored into one vector called as "data"

class MyHandler extends DefaultHandler {
    String rootname;Attributes atr;
    private boolean flag=false;private Vector data;
    public void startElement(String namespaceURI, String localName,
                             String qName, Attributes atts)  {
     rootname=localName;
     atr=atts;

     if(rootname.equalsIgnoreCase("address_component")){
         data=new Vector();
         flag=true;
     }


    }
    public void characters(char[] ch, int start, int length){
        String value=new String(ch,start,length);
        if(flag)
        {
            if(rootname.equalsIgnoreCase("type")){
                data.addElement(value) ;
                System.out.println("++++++++++++++"+value);
            }
            if(rootname.equalsIgnoreCase("long_name")){
                data.addElement(value) ;
                System.out.println("++++++++++++++"+value);
            }
            if(rootname.equalsIgnoreCase("short_name")){
                data.addElement(value) ;
                System.out.println("++++++++++++++"+value);
            }
        }

    }
    public void endElement(String uri, String localName, String qName){
        rootname=localName;
         if(rootname.equalsIgnoreCase("address_component")){
             flag=false;
         }
    }
}

you can find all data into the data vector and also you can find the data onconsole as

++++++++++++++Gujarat

++++++++++++++Gujarat

++++++++++++++administrative_area_level_1

++++++++++++++political

Govindarao Kondala
  • 2,862
  • 17
  • 27
0

Read this tutorial. This will help you to parse xml file using sax parser.

Abhendra Singh
  • 1,959
  • 4
  • 26
  • 46