Parsing XML from the Net - Using the SAXParser

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Re: Parsing XML from the Net - Using the SAXParser

Postby ProfVersaggi » Fri Apr 15, 2011 12:44 pm

Really great stuff! Keep up the good work! It made learning the whole XML Parser thing much easier. :-)
ProfVersaggi
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Apr 15, 2011 12:21 pm

Top

Re: Sync SQLite/Android database table to remote MySQL datab

Postby songotho » Fri Apr 15, 2011 1:49 pm

ajaypatelaj wrote:Can you please explain me.. i am beginner

songotho wrote:Hi,
U try to not connect with your MySQL database and you will know what is the right direction^^.
Alex


The first, when you run the application, the SqLite database will be created also the table is the same on Android phone but will have not any data in tables.
Second, because your phone have connect with MySQL database so the data will be transferred to SQLite database. The theory will be liked this, I don't know what your program is. But I think that you can try to not connect your phone to MySQL database and you can see the results.

Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Re: Parsing XML from the Net - Using the SAXParser

Postby ajaypatelaj » Mon Apr 18, 2011 7:02 am

can any one help me.. why i am getting error... " THe application pasexml stopped unexpectedly " ...??

i write same above 3 file....
ajaypatelaj
Junior Developer
Junior Developer
 
Posts: 22
Joined: Mon Apr 04, 2011 12:54 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby ajaypatelaj » Mon Apr 18, 2011 10:05 am

Hay plusminus.. thanks for this amazing tutorial....

can you please post importing data in sqlite from xml.... it will really help full all guys to build web service .. and all that type App .... Thanks... will eagerly wait for this tut ... :)
ajaypatelaj
Junior Developer
Junior Developer
 
Posts: 22
Joined: Mon Apr 04, 2011 12:54 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby songotho » Wed Apr 20, 2011 5:20 am

ajaypatelaj wrote:Hay plusminus.. thanks for this amazing tutorial....

can you please post importing data in sqlite from xml.... it will really help full all guys to build web service .. and all that type App .... Thanks... will eagerly wait for this tut ... :)


Hi men,

I think that if you want to import data in sqlite from xml that mean you are parsing xml and then you insert in ur sqlite.
You can try to do the tut first and then every can comments as well as modify for you.
Regard,
Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Re: Parsing XML from the Net - Using the SAXParser

Postby monta007 » Sat Apr 23, 2011 1:15 pm

good morning ,

I used the idea of Parsing XML from the Net - Using the SAXParser with successful but i want to parser a file xml External( in web server) contains a spinner in my project android.plz help me.
thnx
monta007
Freshman
Freshman
 
Posts: 2
Joined: Sat Apr 23, 2011 1:03 pm

Top

Re: Parsing XML from the Net - Using the SAXParser

Postby ancientgeek » Mon Apr 25, 2011 3:29 pm

Hey good evening guys,
I have got a question. Say for example I have this URL:http://spark.tp.edu.sg/dmr/mobileapp/databases.xml. And I want to extract data from it and the place the data into arrays and display in listview? How should I go about doing it? And also I realise that there are repeated tags in the xml. How can I choose what I want to retrieve and display it? I really need the help...
ancientgeek
Freshman
Freshman
 
Posts: 5
Joined: Mon Apr 25, 2011 7:33 am

Re: Parsing XML from the Net - Using the SAXParser

Postby amanni82 » Mon Apr 25, 2011 4:43 pm

Hi Plusminus,

Sorry if this has been answered before but i couldn't find an answer, I am trying to read an XML file with reoccurring tags. I want only 4 values from the first and second set of tags. Here is the XML file:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <forecast_information>
  2. <city data="Cardiff"/>
  3. <postal_code data="Cardiff"/>
  4. <latitude_e6 data=""/>
  5. <longitude_e6 data=""/>
  6. <forecast_date data="2011-04-25"/>
  7. <current_date_time data="2011-04-25 14:50:00 +0000"/>
  8. <unit_system data="US"/>
  9. </forecast_information>
  10. <current_conditions>
  11. <condition data="Partly Cloudy"/>
  12. <temp_f data="64"/>
  13. <temp_c data="18"/>
  14. <humidity data="Humidity: 56%"/>
  15. <icon data="/ig/images/weather/partly_cloudy.gif"/>
  16. <wind_condition data="Wind: SW at 7 mph"/>
  17. </current_conditions>
  18. <forecast_conditions>
  19. <day_of_week data="Mon"/>
  20. <low data="45"/>
  21. <high data="72"/>
  22. <icon data="/ig/images/weather/sunny.gif"/>
  23. <condition data="Clear"/>
  24. </forecast_conditions>
  25. <forecast_conditions>
  26. <day_of_week data="Tue"/>
  27. <low data="41"/>
  28. <high data="59"/>
  29. <icon data="/ig/images/weather/mostly_sunny.gif"/>
  30. <condition data="Mostly Sunny"/>
  31. </forecast_conditions>
  32. <forecast_conditions>
  33. <day_of_week data="Wed"/>
  34. <low data="37"/>
  35. <high data="59"/>
  36. <icon data="/ig/images/weather/sunny.gif"/>
  37. <condition data="Clear"/>
  38. </forecast_conditions>
  39. <forecast_conditions>
  40. <day_of_week data="Thu"/>
  41. <low data="43"/>
  42. <high data="57"/>
  43. <icon data="/ig/images/weather/sunny.gif"/>
  44. <condition data="Clear"/>
  45. </forecast_conditions>
  46. </weather>
  47.  
Parsed in 0.008 seconds, using GeSHi 1.0.8.4

I only want the temp, wind, condition values from the current day and the next day, here is my handler class

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.spinner;
  2.  
  3.  
  4.  
  5. import org.xml.sax.Attributes;
  6. import org.xml.sax.SAXException;
  7. import org.xml.sax.helpers.DefaultHandler;
  8.  
  9.  
  10. public class ExampleHandler extends DefaultHandler{
  11.  
  12.         // ===========================================================
  13.         // Fields
  14.         // ===========================================================
  15.        
  16.         private boolean in_forecast_information = false;
  17.         private boolean in_in_current_conditions = false;
  18.         private boolean in_in_forecast_conditions = false;
  19.        
  20.         private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
  21.        
  22.  
  23.         // ===========================================================
  24.         // Getter & Setter
  25.         // ===========================================================
  26.  
  27.         public ParsedExampleDataSet getParsedData() {
  28.                 return this.myParsedExampleDataSet;
  29.         }
  30.  
  31.         // ===========================================================
  32.         // Methods
  33.         // ===========================================================
  34.         @Override
  35.         public void startDocument() throws SAXException {
  36.                 this.myParsedExampleDataSet = new ParsedExampleDataSet();
  37.         }
  38.  
  39.         @Override
  40.         public void endDocument() throws SAXException {
  41.                 // Nothing to do
  42.         }
  43.  
  44.         /** Gets be called on opening tags like:
  45.          * <tag>
  46.          * Can provide attribute(s), when xml was like:
  47.          * <tag attribute="attributeValue">*/
  48.         @Override
  49.         public void startElement(String namespaceURI, String localName,
  50.                         String qName, Attributes atts) throws SAXException {
  51.                 if (localName.equals("forecast_information")) {
  52.                         this.in_forecast_information = true;
  53.                        
  54.                 }else if (localName.equals("current_conditions")) {
  55.                         this.in_in_current_conditions = true;
  56.                 } if (localName.equals("temp_f")) {
  57.                        
  58.                         String attrValue = atts.getValue("data");
  59.                         myParsedExampleDataSet.setCurrtempValue(attrValue);
  60.                        
  61.                        
  62.                 }else if (localName.equals("forecast_conditions")) {
  63.                         this.in_in_forecast_conditions = true;
  64.                 }if (localName.equals("low")) {
  65.                          this.in_in_forecast_conditions=true;}
  66.                 String attrValue1 = atts.getValue("data");
  67.                 myParsedExampleDataSet.setLowValue(attrValue1);
  68.                
  69.        }
  70.        
  71.                
  72.        
  73.         /** Gets be called on closing tags like:
  74.          * </tag> */
  75.         @Override
  76.         public void endElement(String namespaceURI, String localName, String qName)
  77.                         throws SAXException {
  78.                 if (localName.equals("forecast_information")) {
  79.                         this.in_forecast_information = false;
  80.                 } else if (localName.equals("current_conditions")) {
  81.                         this.in_in_current_conditions = false;
  82.                 } else if (localName.equals("forecast_conditions")) {
  83.                         this.in_in_forecast_conditions = false;
  84.                 }
  85.         }
  86.  
  87.         @Override
  88.         public void characters(char ch[], int start, int length) {
  89.                
  90.         }
  91.        
  92.     }
  93.  
  94.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
amanni82
Freshman
Freshman
 
Posts: 3
Joined: Sat Apr 02, 2011 12:04 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby Menor » Mon Apr 25, 2011 9:50 pm

Hello plusminus, nice tutorial.

If i want to load the xml from assets folder, how can i load this?

I've tryed :

URL url = new URL("file:///android_asset/example.xml");


but it doesn't work.
Menor
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Mon Apr 11, 2011 3:45 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby Menor » Tue Apr 26, 2011 2:03 pm

The solution to local xml is:

xr.parse(new InputSource(getResources().getAssets().open("example.xml")));

Thanks
Menor
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Mon Apr 11, 2011 3:45 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby amanni82 » Tue Apr 26, 2011 3:15 pm

Menor wrote:The solution to local xml is:

xr.parse(new InputSource(getResources().getAssets().open("example.xml")));

Thanks

Any idea how to get values from an xml file like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <current_conditions>
  3. <condition data="Clear"/>
  4. <temp_f data="61"/>
  5. <temp_c data="16"/>
  6. <humidity data="Humidity: 68%"/>
  7. <icon data="/ig/images/weather/sunny.gif"/>
  8. <wind_condition data="Wind: E at 13 mph"/>
  9. </current_conditions>
  10. ---------------------------------------------------
  11. <forecast_conditions>
  12. <day_of_week data="Tue"/>
  13. <low data="39"/>
  14. <high data="62"/>
  15. <icon data="/ig/images/weather/mostly_sunny.gif"/>
  16. <condition data="Mostly Sunny"/>
  17. </forecast_conditions>
  18. -----------------------------------------------------
  19. <forecast_conditions>
  20. <day_of_week data="Wed"/>
  21. <low data="37"/>
  22. <high data="59"/>
  23. <icon data="/ig/images/weather/sunny.gif"/>
  24. <condition data="Clear"/>
  25. </forecast_conditions>
  26.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4

I want to get values from only the middle set of tags
amanni82
Freshman
Freshman
 
Posts: 3
Joined: Sat Apr 02, 2011 12:04 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby songotho » Wed Apr 27, 2011 5:45 am

Any idea how to get values from an xml file like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <current_conditions>
  3. <condition data="Clear"/>
  4. <temp_f data="61"/>
  5. <temp_c data="16"/>
  6. <humidity data="Humidity: 68%"/>
  7. <icon data="/ig/images/weather/sunny.gif"/>
  8. <wind_condition data="Wind: E at 13 mph"/>
  9. </current_conditions>
  10. ---------------------------------------------------
  11. <forecast_conditions>
  12. <day_of_week data="Tue"/>
  13. <low data="39"/>
  14. <high data="62"/>
  15. <icon data="/ig/images/weather/mostly_sunny.gif"/>
  16. <condition data="Mostly Sunny"/>
  17. </forecast_conditions>
  18. -----------------------------------------------------
  19. <forecast_conditions>
  20. <day_of_week data="Wed"/>
  21. <low data="37"/>
  22. <high data="59"/>
  23. <icon data="/ig/images/weather/sunny.gif"/>
  24. <condition data="Clear"/>
  25. </forecast_conditions>
  26.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4

I want to get values from only the middle set of tags


U can add the condition when u parse ur xml file like data = Tue.
Regard,
Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Re: Parsing XML from the Net - Using the SAXParser

Postby Menor » Wed Apr 27, 2011 7:33 pm

amanni82 wrote:
Menor wrote:The solution to local xml is:

xr.parse(new InputSource(getResources().getAssets().open("example.xml")));

Thanks

Any idea how to get values from an xml file like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <current_conditions>
  3. <condition data="Clear"/>
  4. <temp_f data="61"/>
  5. <temp_c data="16"/>
  6. <humidity data="Humidity: 68%"/>
  7. <icon data="/ig/images/weather/sunny.gif"/>
  8. <wind_condition data="Wind: E at 13 mph"/>
  9. </current_conditions>
  10. ---------------------------------------------------
  11. <forecast_conditions>
  12. <day_of_week data="Tue"/>
  13. <low data="39"/>
  14. <high data="62"/>
  15. <icon data="/ig/images/weather/mostly_sunny.gif"/>
  16. <condition data="Mostly Sunny"/>
  17. </forecast_conditions>
  18. -----------------------------------------------------
  19. <forecast_conditions>
  20. <day_of_week data="Wed"/>
  21. <low data="37"/>
  22. <high data="59"/>
  23. <icon data="/ig/images/weather/sunny.gif"/>
  24. <condition data="Clear"/>
  25. </forecast_conditions>
  26.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4

I want to get values from only the middle set of tags



Hello

You can get these values...

If i want to get the condition atributes, check your sequence of if and else on "public void startElement" method. Take a look an example:

Code: Select all
if (localName.equals("condition")) {
            String conditionData = atts.getValue("data"));
}


after this, the string "conditionData" will have the value "Clear"...

You'll always get the attributes from the startElement method.
To get the value between the tags (<tag>THIS_VALUE</tag>), you'll need to implement it on the "characters(char ch[], int start, int length)" method...
Menor
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Mon Apr 11, 2011 3:45 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby ancientgeek » Thu Apr 28, 2011 8:16 am

Hey Menor,
Would it be possible if you explain more? Or show an example code on getting the value between the tags based on their attribute values? I'm stuck at trying to extract the values based on the attribute value.
Thanks,
Joel
ancientgeek
Freshman
Freshman
 
Posts: 5
Joined: Mon Apr 25, 2011 7:33 am

Re: Parsing XML from the Net - Using the SAXParser

Postby Menor » Thu Apr 28, 2011 1:21 pm

ancientgeek wrote:Hey Menor,
Would it be possible if you explain more? Or show an example code on getting the value between the tags based on their attribute values? I'm stuck at trying to extract the values based on the attribute value.
Thanks,
Joel


I'll show you an example.

The xml that i want to red is:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0"?>
  2. <fairs>
  3.         <fair>
  4.                 <name>FAIR ONE</name>
  5.                 <date>April 29</date>
  6.                 <active>true</active>
  7.                 <active_image>image_active</active_image>
  8.                 <inactive_image>image_inactive</inactive_image>
  9.                 <map>map/index.html</map>
  10.                 <info>info.xml</info>
  11.                 <booths>
  12.                         <booth name="Booth01" target="booth01/index.html">
  13.                                 <topics>
  14.                                         <topic name="x">x.xml</topic>
  15.                                         <topic name="y">y.xml</topic>
  16.                                         <topic name="z">z.xml</topic>
  17.                                 </topics>
  18.                         </booth>
  19.                         <booth name="Booth02" target="booth02/index.html">
  20.                                 <topics>
  21.                                         <topic name="a">a.xml</topic>
  22.                                         <topic name="b">b.xml</topic>
  23.                                         <topic name="c">c.xml</topic>
  24.                                 </topics>
  25.                         </booth>
  26.                         <booth name="Booth03" target="booth03/index.html">
  27.                                 <topics>
  28.                                         <topic name="d">d.xml</topic>
  29.                                         <topic name="e">e.xml</topic>
  30.                                         <topic name="f">f.xml</topic>
  31.                                 </topics>
  32.                         </booth>
  33.                         <booth name="Booth04" target="booth04/index.html">
  34.                                 <topics>
  35.                                         <topic name="g">g.xml</topic>
  36.                                         <topic name="h">h.xml</topic>
  37.                                         <topic name="i">i.xml</topic>
  38.                                 </topics>
  39.                         </booth>
  40.                 </booths>
  41.         </fair>
  42.         ...
  43. </fairs>
  44.  
Parsed in 0.009 seconds, using GeSHi 1.0.8.4



My handler class:
Code: Select all
package mypackage;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import mypackage.FairBooth;
import mypackage.FairDataManager;
import mypackage.FairTopic;

public class FairListHandler extends DefaultHandler {

   private boolean in_fairs = false;
   private boolean in_fair = false;
   private boolean in_name = false;
   private boolean in_date = false;
   private boolean in_active = false;
   private boolean in_activeimage = false;
   private boolean in_inactiveimage = false;
   private boolean in_map = false;
   private boolean in_info = false;
   private boolean in_booths = false;
   private boolean in_booth = false;
   private boolean in_topics = false;
   private boolean in_topic = false;

        // here is put all fairs. when the first fair (on this example, we have just one fair on xml, but if there are many, this code also will work
       
        // My data structure to put data
        private ArrayList<FairDataManager> array = new ArrayList<FairDataManager>();
        private FairDataManager parsedData = new FairDataManager();
   private FairBooth fairBooth = new FairBooth();
   private FairTopic fairTopic = new FairTopic();


   public ArrayList getParsedData() {
      return this.array;
   }

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

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


   @Override
   public void startElement(String namespaceURI, String localName,
         String qName, Attributes atts) throws SAXException {
      if (localName.equals("fairs")) {
         this.in_fairs = true;
      } else if (localName.equals("fair")) {
         this.in_fair = true;
      } else if (localName.equals("name")) {
         this.in_name = true;
      } else if (localName.equals("date")) {
         this.in_date = true;
      } else if (localName.equals("active")) {
         this.in_active = true;
      } else if (localName.equals("active_image")) {
         this.in_activeimage = true;
      } else if (localName.equals("inactive_image")) {
         this.in_inactiveimage = true;
      } else if (localName.equals("map")) {
         this.in_map = true;
      } else if (localName.equals("info")) {
         this.in_info = true;
      } else if (localName.equals("booths")) {
         this.in_booths = true;
      } else if (localName.equals("booth")) {
         this.in_booth = true;
         fairBooth = new FairBooth();
                        // here i get the attributes values from <booth> tag
         fairBooth.setBoothName(atts.getValue("name"));
         fairBooth.setBoothPath(atts.getValue("target"));
      } else if (localName.equals("topics")) {
         this.in_topics = true;
      } else if (localName.equals("topic")) {
         this.in_topic = true;
         fairTopic = new FairTopic();
         fairTopic.setTopicName(atts.getValue("name"));

      }
   }

   @Override
   public void endElement(String namespaceURI, String localName, String qName)
         throws SAXException {
      if (localName.equals("fairs")) {
         this.in_fairs = false;
      } else if (localName.equals("fair")) {
         this.in_fair = false;
                        //when the fair ends, add the fair on fair array
         array.add(parsedData);
         parsedData = new FairDataManager();
         fairBooth = new FairBooth();
         fairTopic = new FairTopic();
      } else if (localName.equals("name")) {
         this.in_name = false;
      } else if (localName.equals("date")) {
         this.in_date = false;
      } else if (localName.equals("active")) {
         this.in_active = false;
      } else if (localName.equals("active_image")) {
         this.in_activeimage = false;
      } else if (localName.equals("inactive_image")) {
         this.in_inactiveimage = false;
      } else if (localName.equals("map")) {
         this.in_map = false;
      } else if (localName.equals("info")) {
         this.in_info = false;
      } else if (localName.equals("booths")) {
         this.in_booths = false;
      } else if (localName.equals("booth")) {
         this.in_booth = false;
      } else if (localName.equals("topics")) {
         this.in_topics = false;
                        // when topics ends, add the booth on fair
         parsedData.addBooth(fairBooth);
         fairBooth = new FairBooth();

      } else if (localName.equals("topic")) {
         this.in_topic = false;
                        // when the topic ends, add topic on booth
         fairBooth.addTopic(fairTopic);
         fairTopic = new FairTopic();

      }
   }

   @Override
   public void characters(char ch[], int start, int length) {
      if (this.in_name) {
                       // here i get the value between the tag <name>THIS_VALUE</name>
         parsedData.setName(new String(ch, start, length));
      }
      if (this.in_date) {
                         // here i get the value between the tag <date>THIS_VALUE</date>
         parsedData.setDate(new String(ch, start, length));
      }
      if (this.in_active) {
                        // here i get the value between the tag <active>THIS_VALUE</active>
         String active = new String(ch, start, length);
         if (active.equals("true")) {
            parsedData.setActive(true);
         } else {
            parsedData.setActive(false);
         }
      }
      if (this.in_activeimage) {
                         // here i get the value between the tag <active_image>THIS_VALUE</active_image>
         parsedData.setActiveImage(new String(ch, start, length));
      }
      if (this.in_inactiveimage) {
                        // here i get the value between the tag <inactive_image>THIS_VALUE</inactive_image>
         parsedData.setInactiveImage(new String(ch, start, length));
      }
      if (this.in_map) {
                        // here i get the value between the tag <map>THIS_VALUE</map>
         parsedData.setMapUrl(new String(ch, start, length));
      }
      if (this.in_info) {
         // here i get the value between the tag <info>THIS_VALUE</info>
                        parsedData.setInfoUrl(new String(ch, start, length));
      }
      if (this.in_topic) {
         // here i get the value between the tag <topic>THIS_VALUE</topic>
                        fairTopic.setTopicPath(new String(ch, start, length));
      }

   }
}



I hope that this example help you...

if you still have questions, ask me.

;)
Menor
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Mon Apr 11, 2011 3:45 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests