parsing xml from network database

General Android Books.
Also feel free to comment, improve and contribute to AndBook, the free Android Learners book.

parsing xml from network database

Postby SivaKSankar » Tue Apr 05, 2011 9:15 am

when i was searching to find an example for parsing an xml from a URL which has a database of its own i found the following example posted by PlusMinus

http://www.anddev.org/parsing_xml_from_the_net_-_using_the_saxparser-t353.html

i tried using the same but in my app it returns as null n null

Following is the xml which i try to parse
Code: Select all
<Searchdata><Searchdata><id>1</id><First_name>A</First_name><Last_name>Apollo</Last_name></Searchdat></Searchdata>


following is my main class fil
Code: Select all
URL url = new URL (RestingSpotConstants2.RESTINGSPOT_SEARCH_URL+a
              +RestingSpotConstants2.RESTINGSPOT_FIRSTNAME2+b
              +RestingSpotConstants2.RESTINGSPOT_LASTNAME2+c);
           
           /** Handling XML */
            saxparserfactory = SAXParserFactory.newInstance();
            saxparser = saxparserfactory.newSAXParser();
            xmlreader = saxparser.getXMLReader();
           
           
            /* Create a new ContentHandler and apply it to the XML-Reader*/
            ForListXMLHandler forlistmyhandler = new ForListXMLHandler();   
            xmlreader.setContentHandler(forlistmyhandler);

           
            /* Parse the xml-data from our URL. */
            xmlreader.parse(new InputSource(url.openStream()));
            /* Parsing has finished. */
           
            /* Our ExampleHandler now provides the parsed data to us. */
            ParsedDataSet parsedDataSet = forlistmyhandler.getParsedData();
            tv.setText(parsedDataSet.toString());
           
       }
        catch(Exception e)
        {
           Log.e("searchlist",""+e);
        }
       
       
      this.setContentView(tv);


This is the forlistXML handler class
Code: Select all
public class ForListXMLHandler extends DefaultHandler
{   
    private boolean in_outertag = false;
    private boolean in_innertag = false;
    private boolean in_First_name = false;
    private boolean in_Last_name = false;
   
    private ParsedDataSet myParsedDataSet = new ParsedDataSet();

    public ParsedDataSet getParsedData()
    {
            return this.myParsedDataSet;
    }

    @Override
    public void startDocument() throws SAXException
    {
            this.myParsedDataSet = new ParsedDataSet();
    }

    @Override
    public void endDocument() throws SAXException
    {
            // Nothing to do
    }

   
    public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException
    {
            if (localName.equals("Searchdata"))
            {
                    this.in_outertag = true;
            }
            else if (localName.equals("Searchdata"))
            {
                    this.in_innertag = true;
            }
            else if (localName.equals("First_name"))
            {
                    this.in_First_name = true;
            }
            else if (localName.equals("Last_name"))
            {
                   this.in_Last_name = true;
            }
    }
   
    /** Gets be called on closing tags like:
     * </tag> */
    @Override
    public void endElement(String namespaceURI, String localName, String qName)throws SAXException
   {
            if (localName.equals("Searchdata"))
            {
                    this.in_outertag = false;
            }
            else if (localName.equals("Searchdata"))
            {
                    this.in_innertag = false;
            }
            else if (localName.equals("First_name"))
            {
                    this.in_First_name = false;
            }
            else if (localName.equals("Last_name"))
            {
                    // Nothing to do here
            }
    }
   
    /** Gets be called on the following structure:
     * <tag>characters</tag> */
    @Override
    public void characters(char ch[], int start, int length)
    {
            if(this.in_First_name)
            {
               myParsedDataSet.setfirstname(new String(ch, start, length));
            }
            if(this.in_Last_name)
            {
               myParsedDataSet.setlastname(new String(ch, start, length));
            }
    }


This is my class for parsed data set
Code: Select all
public class ForListXMLHandler extends DefaultHandler
{   
    private boolean in_outertag = false;
    private boolean in_innertag = false;
    private boolean in_First_name = false;
    private boolean in_Last_name = false;
   
    private ParsedDataSet myParsedDataSet = new ParsedDataSet();

    public ParsedDataSet getParsedData()
    {
            return this.myParsedDataSet;
    }

    @Override
    public void startDocument() throws SAXException
    {
            this.myParsedDataSet = new ParsedDataSet();
    }

    @Override
    public void endDocument() throws SAXException
    {
            // Nothing to do
    }

   
    public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException
    {
            if (localName.equals("Searchdata"))
            {
                    this.in_outertag = true;
            }
            else if (localName.equals("Searchdata"))
            {
                    this.in_innertag = true;
            }
            else if (localName.equals("First_name"))
            {
                    this.in_First_name = true;
            }
            else if (localName.equals("Last_name"))
            {
                   this.in_Last_name = true;
            }
    }
   
    /** Gets be called on closing tags like:
     * </tag> */
    @Override
    public void endElement(String namespaceURI, String localName, String qName)throws SAXException
   {
            if (localName.equals("Searchdata"))
            {
                    this.in_outertag = false;
            }
            else if (localName.equals("Searchdata"))
            {
                    this.in_innertag = false;
            }
            else if (localName.equals("First_name"))
            {
                    this.in_First_name = false;
            }
            else if (localName.equals("Last_name"))
            {
                    // Nothing to do here
            }
    }
   
    /** Gets be called on the following structure:
     * <tag>characters</tag> */
    @Override
    public void characters(char ch[], int start, int length)
    {
            if(this.in_First_name)
            {
               myParsedDataSet.setfirstname(new String(ch, start, length));
            }
            if(this.in_Last_name)
            {
               myParsedDataSet.setlastname(new String(ch, start, length));
            }
    }



pls tell me where i am going wrong....very urgent for me
SivaKSankar
Freshman
Freshman
 
Posts: 3
Joined: Tue Apr 05, 2011 8:38 am

Top

Return to Android Books

Who is online

Users browsing this forum: No registered users and 1 guest