Parsing XML from the Net - Using the SAXParser

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

Postby Rohini » Wed Feb 24, 2010 10:23 am

hi i didnt get the extracted string in my output.. may i know why?
Rohini
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Feb 24, 2010 10:18 am

Top

Re: Parsing XML from the Net - Using the SAXParser

Postby my7h1c » Wed Feb 24, 2010 5:24 pm

pradeep_android wrote:i tried ur first xml parsing.
it returned this error

"permission denied(may be missing Internet Persmission)

i tried this saving xml in local host still got the same error. How to resolve it?
kindly help with this plz

urgent

-thanks in advance
you need to add <uses-permission android:name="android.permission.INTERNET" /> to the android manifest
my7h1c
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Feb 24, 2010 9:38 am

Postby my7h1c » Wed Feb 24, 2010 7:54 pm

can some please help with my question I want to be able to put the text we got from the xml into an xml layout that I can use in a variety of activity's.
my7h1c
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Feb 24, 2010 9:38 am

Re: Parsing XML from the Net - Using the SAXParser

Postby freaky_flash » Fri Mar 05, 2010 7:29 am

hey plusminus,
can you help me in parsing image tags in XHTML and displaying those images in Android emulator? Plz. reply asap....
freaky_flash
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Feb 20, 2010 4:59 pm

Postby epicFail » Fri Mar 12, 2010 2:17 am

Excellent tutorial!

I am alittle stuck... I need to parse an xml which contains:
<article xml:id="0">

Any idea on how to do this? I can't seem to get it to work.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                  if(this.in_article)
  2.  
  3.                         myParsedExampleDataSet.setId(atts.getValue("xml:", "id"));
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
epicFail
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Mar 12, 2010 1:58 am

Postby Jonasm » Mon Mar 29, 2010 4:32 pm

Tutorial is great and everything seems to be working perfectly, but i have one problem... in handler file when i write @Override near the public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException{} i get an error: public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException. And the only solution which eclipse suggest is to remove @Override, but then this method doesn't work. here is my code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     /** Gets be called on opening tags like:
  3.  
  4.      * <tag>
  5.  
  6.      * Can provide attribute(s), when xml was like:
  7.  
  8.      * <tag attribute="attributeValue">*/
  9.  
  10.     @Override
  11.  
  12.     public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
  13.  
  14.         if(localName.equals("cases")){
  15.  
  16.                 this.in_cases = true;
  17.  
  18.         }else if(localName.equals("case")){
  19.  
  20.                 this.in_case = true;
  21.  
  22.         }else if(localName.equals("files")){
  23.  
  24.                 this.in_files = true;
  25.  
  26.         }else if(localName.equals("file")){
  27.  
  28.                 this.in_file = true;
  29.  
  30.         }else if(localName.equals("name")){
  31.  
  32.                 this.in_name = true;
  33.  
  34.         }else if(localName.equals("title")){
  35.  
  36.                 this.in_title = true;
  37.  
  38.         }else if(localName.equals("id")){
  39.  
  40.                 this.in_id = true;
  41.  
  42.         }
  43.  
  44.     }
  45.  
  46.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Maybe somedy has any idea where the problem is? everything is ok with other functions.

[hr]

Edit: I found my mistake. It was wrong Attributes import. I had to import org.xml.sax.Attributes
Jonasm
Freshman
Freshman
 
Posts: 4
Joined: Wed Mar 17, 2010 8:45 pm

Top

Help Me Please

Postby mmgill » Mon Mar 29, 2010 9:51 pm

Hi,
Iv been having some trouble trying to parse an XML file that looks like this:

http://local.yahooapis.com/LocalSearchS ... &results=3

All that i want to get is the name of the banks within the <Title> tags and add them to a ListView. I have been looking around trying to find a solution but am about to go nuts. Could someone please give me example code that would add the titles to a ListView so i can grasp the concept of parsing XML. Thanks for all the help and preventing me from going insane.

Mg
mmgill
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Mar 04, 2010 8:40 pm

Postby Jonasm » Tue Mar 30, 2010 7:58 am

All you have to do, is something like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4. public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
  5.  
  6.      if(localName.equals("Title")){
  7.  
  8.           this.in_title = true;
  9.  
  10.      }
  11.  
  12. }
  13.  
  14. public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
  15.  
  16.         if(localName.equals("Title")){
  17.  
  18.                 this.in_title = false;
  19.  
  20.         }
  21.  
  22. }
  23.  
  24.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


and

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void characters(char ch[], int start, int length) {
  3.  
  4.         if(this.in_title){
  5.  
  6.                 String title = new String(ch, start, length));
  7.  
  8.                 //add string title to your list
  9.  
  10.         }
  11.  
  12. }
  13.  
  14.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


I hope this helps.
Jonasm
Freshman
Freshman
 
Posts: 4
Joined: Wed Mar 17, 2010 8:45 pm

Xml parsing

Postby mmgill » Tue Mar 30, 2010 7:27 pm

hi,

thanks for getting back to me so soon. I added these snippets into my code but am still not getting the results i need. I dont want the titles to be added to the a string of items but to a scrollable listview. I have been trying to do it may different ways but think that there might be a problem with how the xml is formed. can someone take a look at it and see if it would even work if i wanted to add it to a list view by <title>?

http://local.yahooapis.com/LocalSearchS ... &results=4

Also, is there anywhere that has a good example of how to get info, like the title, out and added to a listview?
!
Thanks for the help guys!]

Mg
mmgill
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Mar 04, 2010 8:40 pm

Postby Jonasm » Tue Mar 30, 2010 7:37 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. String title = new String(ch, start, length));
  3.  
  4.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

This line is only for getting title, i mean it forms string. Later you can add that string to let's say ArrayList and then to your ListView from ArrayList.
Jonasm
Freshman
Freshman
 
Posts: 4
Joined: Wed Mar 17, 2010 8:45 pm

xml

Postby mmgill » Tue Mar 30, 2010 7:38 pm

hi,

Im sorry this is where im getting lost. I am not sure how to go about adding it to an array then to listview

thanks
mmgill
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Mar 04, 2010 8:40 pm

XML getting there..:)

Postby mmgill » Tue Mar 30, 2010 8:52 pm

So this is what i have so far

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.google.it;
  3.  
  4.  
  5.  
  6. import java.util.jar.Attributes;
  7.  
  8.  
  9.  
  10. import org.xml.sax.SAXException;
  11.  
  12. import org.xml.sax.helpers.DefaultHandler;
  13.  
  14.  
  15.  
  16. public class SAXparserHandler extends DefaultHandler{
  17.  
  18.            
  19.  
  20.     private boolean in_title = false;
  21.  
  22.    
  23.  
  24.     private ParsedDataSet myParsedDataSet = new ParsedDataSet();
  25.  
  26.  
  27.  
  28.     public ParsedDataSet getParsedData() {
  29.  
  30.          return this.myParsedDataSet;
  31.  
  32.     }
  33.  
  34.  
  35.  
  36.     @Override
  37.  
  38.     public void startDocument() throws SAXException {
  39.  
  40.          this.myParsedDataSet = new ParsedDataSet();
  41.  
  42.     }
  43.  
  44.  
  45.  
  46.     @Override
  47.  
  48.     public void endDocument() throws SAXException {
  49.  
  50.          // Nothing to do
  51.  
  52.     }
  53.  
  54.  
  55.  
  56.     public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
  57.  
  58.      if(localName.equals("Title")){
  59.  
  60.           this.in_title = true;
  61.  
  62.      }    
  63.  
  64.     }
  65.  
  66.  
  67.  
  68.     @Override
  69.  
  70.     public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
  71.  
  72.      if(localName.equals("Title")){
  73.  
  74.           this.in_title = false;
  75.  
  76.           }
  77.  
  78.     }
  79.  
  80.    
  81.  
  82.  
  83.  
  84.     @Override
  85.  
  86.     public void characters(char ch[], int start, int length) {
  87.  
  88.      if(this.in_title){
  89.  
  90.           myParsedDataSet.addTitle(new String(ch, start, length));
  91.  
  92.      }
  93.  
  94.     }
  95.  
  96. }
  97.  
  98.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


and
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.google.it;
  3.  
  4.  
  5.  
  6. import java.util.ArrayList;
  7.  
  8.  
  9.  
  10. public class ParsedDataSet {
  11.  
  12.     ArrayList<String> titles = new ArrayList<String>();
  13.  
  14.    
  15.  
  16.     public void addTitle(String tit){
  17.  
  18.          titles.add(tit);
  19.  
  20.     }
  21.  
  22.    
  23.  
  24.     public ArrayList<String> getTitles(){
  25.  
  26.          return titles;
  27.  
  28.     }
  29.  
  30. }
  31.  
  32.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


and..
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.google.it;
  3.  
  4.  
  5.  
  6. import java.net.URL;
  7.  
  8. import java.util.ArrayList;
  9.  
  10.  
  11.  
  12. import javax.xml.parsers.SAXParserFactory;
  13.  
  14.  
  15.  
  16. import org.xml.sax.InputSource;
  17.  
  18. import org.xml.sax.XMLReader;
  19.  
  20.  
  21.  
  22.  
  23.  
  24. public class SAXParser {
  25.  
  26.     ParsedDataSet parsedDataSet;
  27.  
  28.     URL url;
  29.  
  30.    
  31.  
  32.     public SAXParser(URL u){
  33.  
  34.          url = u;
  35.  
  36.          parse();
  37.  
  38.     }
  39.  
  40.    
  41.  
  42.     public void parse(){
  43.  
  44.          try {
  45.  
  46.            SAXParserFactory spf = SAXParserFactory.newInstance();
  47.  
  48.            javax.xml.parsers.SAXParser sp = spf.newSAXParser();
  49.  
  50.  
  51.  
  52.            XMLReader xr = sp.getXMLReader();
  53.  
  54.          
  55.  
  56.            SAXparserHandler myHandler = new SAXparserHandler();
  57.  
  58.            xr.setContentHandler(myHandler);
  59.  
  60.          
  61.  
  62.            xr.parse(new InputSource(url.openStream()));
  63.  
  64.  
  65.  
  66.            parsedDataSet = myHandler.getParsedData();
  67.  
  68.          
  69.  
  70.       } catch (Exception e) {
  71.  
  72.        //do what you want
  73.  
  74.       }
  75.  
  76.     }
  77.  
  78.     //get parsed data
  79.  
  80.     public ArrayList<String> getParsedTitles(){
  81.  
  82.          return parsedDataSet.getTitles();
  83.  
  84.     }
  85.  
  86. }
  87.  
  88.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


and mainactivity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.google.it;
  3.  
  4.  
  5.  
  6. import java.net.MalformedURLException;
  7.  
  8. import java.net.URL;
  9.  
  10.  
  11.  
  12. import android.app.Activity;
  13.  
  14. import android.os.Bundle;
  15.  
  16.  
  17.  
  18. public class TheMain extends Activity{
  19.  
  20.     /** Called when the activity is first created. */
  21.  
  22.        
  23.  
  24.         static String feedUrlString = "http://local.yahooapis.com/LocalSearchService/V3/localSearch?appid=YahooDemo&query=bank&latitude=40.5141667&longitude=-88.9905556&radius=2&results=2";
  25.  
  26.         private URL feedUrl;
  27.  
  28.        
  29.  
  30.        
  31.  
  32.     @Override
  33.  
  34.     public void onCreate(Bundle savedInstanceState) {
  35.  
  36.         super.onCreate(savedInstanceState);
  37.  
  38.         setContentView(R.layout.main);
  39.  
  40.         try {
  41.  
  42.                         this.feedUrl = new URL(feedUrlString);
  43.  
  44.                 } catch (MalformedURLException e) {
  45.  
  46.                         // TODO Auto-generated catch block
  47.  
  48.                         e.printStackTrace();
  49.  
  50.                 }
  51.  
  52.         SAXParser parser = new SAXParser(feedUrl);
  53.  
  54.         ArrayList<String> titlesList = parser.getParsedTitles();
  55.  
  56.     }
  57.  
  58. }
  59.  
  60.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


With some help i think that this should allow me to add the arraylist in to a listview. Does this sound/look correct? Thanks for the help everyone. Really helps me understand the material.

Mg
Attachments
SaxParse.zip
(30.26 KiB) Downloaded 83 times
mmgill
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Mar 04, 2010 8:40 pm

Postby Johan Degraeve » Tue Mar 30, 2010 10:09 pm

Using a small open source package :

http://code.google.com/p/easyxmldata/

This is still under construction, needs a bit more documentation and will sure be updated in the future.

Using this package and following code, I find four banks :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package examplepackagename;
  3.  
  4.  
  5.  
  6. import java.util.ArrayList;
  7.  
  8.  
  9.  
  10. import net.johandegraeve.easyxmldata.XMLElement;
  11.  
  12. import net.johandegraeve.easyxmldata.EasyXMLDataParser;
  13.  
  14.  
  15.  
  16. import org.xml.sax.SAXParseException;
  17.  
  18.  
  19.  
  20. public class main {
  21.  
  22.         public static void main (String[] args)   {
  23.  
  24.             XMLElement result = null;
  25.  
  26.             String exceptionresult;
  27.  
  28.             ArrayList<String> resultStrings = new ArrayList<String>();
  29.  
  30.            
  31.  
  32.             EasyXMLDataParser myParser = new EasyXMLDataParser(
  33.  
  34.                     null,null);
  35.  
  36.             try {
  37.  
  38.                     result = myParser.parse("http://local.yahooapis.com/LocalSearchService/V3/localSearch?appid=YahooDemo&query=bank&latitude=40.5141667&longitude=-88.9905556&radius=2&results=4");
  39.  
  40.                
  41.  
  42.             } catch (SAXParseException e) {
  43.  
  44.                         exceptionresult = e.toString() +
  45.  
  46.                     "\n" +
  47.  
  48.                     "LineNumber = " +   e.getLineNumber() + "\n" +
  49.  
  50.                     "ColumnNumber = " +   e.getColumnNumber() + "\n" +
  51.  
  52.                     "PublicId = " +   e.getPublicId() + "\n" +
  53.  
  54.                     "SystemId = " +   e.getSystemId() + "\n";
  55.  
  56.                    
  57.  
  58.  
  59.  
  60.                 System.out.printf(exceptionresult);
  61.  
  62.             }
  63.  
  64.            
  65.  
  66.             getText(resultStrings, result);
  67.  
  68.             for (int i = 0;i < resultStrings.size();i++)
  69.  
  70.                 System.out.println(resultStrings.get(i));
  71.  
  72.         }
  73.  
  74.        
  75.  
  76.         static private void getText(ArrayList<String> list, XMLElement result) {
  77.  
  78.             String text;
  79.  
  80.            
  81.  
  82.             if (result.getTagName() != null)
  83.  
  84.                 if (result.getTagName().equals("Title")) {
  85.  
  86.                 text = result.getText();
  87.  
  88.                 if (text != null)
  89.  
  90.                     if (text != "")
  91.  
  92.                         list.add(text);
  93.  
  94.                 }
  95.  
  96.            
  97.  
  98.             ArrayList<XMLElement> children = result.getChildren();
  99.  
  100.             if (children != null)
  101.  
  102.                 for (int i = 0;i < children.size();i++)
  103.  
  104.                     getText(list, children.get(i));
  105.  
  106.         }
  107.  
  108. }
  109.  
  110.  
  111.  
  112.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


the result is :
US Bank
Commerce Bank ATM
Commerce Bank
US Bank
regards,

Johan
Johan Degraeve
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Oct 27, 2009 1:50 pm
Location: Belgium

XML!!!ahh

Postby mmgill » Tue Mar 30, 2010 11:29 pm

hi,

Iv gotten a little bit farther (have the code to add the array to a listview) but have a problem somewhere.. Can someone please take a look and maybe see something that i dont. THANKS!! big help

Regards,

MG
Attachments
SaxParse.zip
(30.96 KiB) Downloaded 62 times
mmgill
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Mar 04, 2010 8:40 pm

XML Help

Postby trickinsk8er » Tue Mar 30, 2010 11:51 pm

I needed some help on parsing some xml from a yahoo web service. I need to to run through the XML and find certain tags such as title, address, phone, etc. Right now I just want it to find the titles. It seems like I need an array to put the tags in but I am having trouble. In the end it needs to be displayed in a listview so it can be clicked on and looked at. I need some help on how to do this exactly. Here is my code below: Please let me know what I need to add take out or any changes I need, thanks.
Attachments
XML Parsing.zip
(3.32 KiB) Downloaded 69 times
trickinsk8er
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Mar 30, 2010 11:37 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests