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 padde » Sat Jun 26, 2010 7:38 am

You change the same Object (myParsedExampleDataSet) over and over again and add it to you ArrayList.
This leaves you with a list of X references to the same object.

To fix this move this line..
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.myParsedExampleDataSet = new ParsedExampleDataSet();
  2.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

from your "startDocument" method to your "startElement" method where you set "in_innertag" to true.

Note:
Your handler dont seem to match your XML. In the XML you have tags ("Drzava" and "NazivAng") the
handler doesnt take care of and on the other side your handler handles tags ("Oznaka" and "Sreden") that
doesnt exist in your XML.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Top

Re: Parsing XML from the Net - Using the SAXParser

Postby alex_tm_ro » Tue Jun 29, 2010 12:58 pm

Hi plusminus.

I am new to android. I did some applications so far that i have on the market: eloMaze and eloSENS.
Now i want to do a rss reader. Actually it will read from only one source.
The thing is that the XML returned has images and other things.

Can you modify your code form here to make images show up in the list.

Something like:

IMAGE | Description

I am sure other noob devs like myself would find this very interesting.

Thank you very much.

Alex
alex_tm_ro
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jun 29, 2010 12:53 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby vaishumol » Wed Jun 30, 2010 12:29 pm

Hi friends,
Can anyone help me how to parse the following kinda xml Tags?
Im not getting proper output. And do not know where is my mistake. Please help me.

This is my sample.xml code :

Code: Select all
<response>
<lf:request_type>make</lf:request_type>
<lf:response_code>0</lf:response_code>

<lf:result type="make" count="94">

<lf:item id="1">
<lf:attr name="make">AC</lf:attr>
<lf:attr name="listing_count">7</lf:attr>
</lf:item>

<lf:item id="2">
<lf:attr name="make">ACURA</lf:attr>
<lf:attr name="listing_count">32567</lf:attr>
</lf:item>
</lf:result>
</response>


And her is my XML parser code :
Code: Select all
public class MakeXMLHandler extends DefaultHandler {
   
   
   //test tags see if we have to parse the whole document
   //or only the part we want ???
   
   private boolean result = false;
   private boolean item = false;
   private boolean id = false;
   private boolean attr = false;
   private boolean name = false;
   private boolean maker = false;
   private boolean count = false;   
   
   private ParsedDataSet peds = new ParsedDataSet();
   
   public ParsedDataSet getParsedData(){
      return this.peds;
   }
   
   @Override
   public void startDocument() throws SAXException {
      this.peds = new ParsedDataSet();
   }
   
   @Override
   public void endDocument() throws SAXException {
      //nothing to do here
   }
   
   /*
    * Gets be called on opening tags like
    * <tag>
    * Can provide attribute(s), when xml was like:
    * <tag attribute="attributeValue">*/
    public void startElement(String namespaceURI, String localName,
                         String qName, Attributes atts)throws SAXException{
      
       if(localName.equals("result")){
          this.result = true;
         
       }else if(localName.equals("item")){
          this.item = true;
          this.id = true;         
       }else if(localName.equals("attr")){
          String makerattr = atts.getValue("name");
          if (makerattr.equals("make")) {
             this.maker = true;
          }else if(makerattr.equals("listing_count")){
             this.count = true;
          }
         
       }
      
      
    }
   
    //Called on closing tags like </tag>
    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException{
      
      
       if(localName.equals("result")){
          this.result = false;
       }else if(localName.equals("item")){
          this.item = false;
         
       }else if(localName.equals("attr")){
          this.attr = false;
       }
      
      
    }
   
    /** Gets be called on the following structure:
      * <tag>characters</tag> */
     @Override
    public void characters(char ch[], int start, int length) {
       
        if(this.item){
          
           peds.setId(new String(ch, start, length));
        }
        if(this.maker) {
          // peds.setMaker(new String(ch, start, length));
        }
        if(this.count) {
           peds.setListCount(new String(ch, start, length));
        }
   
    }

}


Here is my getter setter method code :
Code: Select all
public class ParsedDataSet {
    private String result = null;
    private String id=null;
    private String attr=null;
    private String maker=null;
    private String count=null;
   

    public String getResult() {
         return result;
    }
    public void setResult(String result) {
         this.result = result;
    }
    public void setId(String id){
       
       this.id=id;
       System.out.println("Id : " + this.id);
    }
   
    public String getAttr(){
       return attr;
    }
    public void setMaker(String maker){
       this.maker = maker;
       System.out.println("maker : " + this.maker);
    }
    public void setListCount(String count){
       this.count=count;
       
    }
       
   
    public String toString(){
       System.out.println("To string :" + this.maker);
       
       return this.maker;
                   
    }
}


And im getting the following output

System.out(): Id :
: INFO/System.out(): Id :
: INFO/System.out(): Id : AC
: INFO/System.out(): maker : AC
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id : 7
: INFO/System.out(): maker : 7
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): maker :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id : ACURA
: INFO/System.out(): maker : ACURA
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id : 32567
: INFO/System.out(): maker : 32567
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): maker :
: INFO/System.out(): maker :
: INFO/System.out(): To string :null

Please help me friends .
vaishumol
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Sun Jan 03, 2010 6:33 am

Re: Parsing XML from the Net - Using the SAXParser

Postby songotho » Thu Jul 01, 2010 1:13 pm

vaishumol wrote:Hi friends,
Can anyone help me how to parse the following kinda xml Tags?
Im not getting proper output. And do not know where is my mistake. Please help me.

This is my sample.xml code :

Code: Select all
<response>
<lf:request_type>make</lf:request_type>
<lf:response_code>0</lf:response_code>

<lf:result type="make" count="94">

<lf:item id="1">
<lf:attr name="make">AC</lf:attr>
<lf:attr name="listing_count">7</lf:attr>
</lf:item>

<lf:item id="2">
<lf:attr name="make">ACURA</lf:attr>
<lf:attr name="listing_count">32567</lf:attr>
</lf:item>
</lf:result>
</response>


And her is my XML parser code :
Code: Select all
public class MakeXMLHandler extends DefaultHandler {
   
   
   //test tags see if we have to parse the whole document
   //or only the part we want ???
   
   private boolean result = false;
   private boolean item = false;
   private boolean id = false;
   private boolean attr = false;
   private boolean name = false;
   private boolean maker = false;
   private boolean count = false;   
   
   private ParsedDataSet peds = new ParsedDataSet();
   
   public ParsedDataSet getParsedData(){
      return this.peds;
   }
   
   @Override
   public void startDocument() throws SAXException {
      this.peds = new ParsedDataSet();
   }
   
   @Override
   public void endDocument() throws SAXException {
      //nothing to do here
   }
   
   /*
    * Gets be called on opening tags like
    * <tag>
    * Can provide attribute(s), when xml was like:
    * <tag attribute="attributeValue">*/
    public void startElement(String namespaceURI, String localName,
                         String qName, Attributes atts)throws SAXException{
      
       if(localName.equals("result")){
          this.result = true;
         
       }else if(localName.equals("item")){
          this.item = true;
          this.id = true;         
       }else if(localName.equals("attr")){
          String makerattr = atts.getValue("name");
          if (makerattr.equals("make")) {
             this.maker = true;
          }else if(makerattr.equals("listing_count")){
             this.count = true;
          }
         
       }
      
      
    }
   
    //Called on closing tags like </tag>
    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException{
      
      
       if(localName.equals("result")){
          this.result = false;
       }else if(localName.equals("item")){
          this.item = false;
         
       }else if(localName.equals("attr")){
          this.attr = false;
       }
      
      
    }
   
    /** Gets be called on the following structure:
      * <tag>characters</tag> */
     @Override
    public void characters(char ch[], int start, int length) {
       
        if(this.item){
          
           peds.setId(new String(ch, start, length));
        }
        if(this.maker) {
          // peds.setMaker(new String(ch, start, length));
        }
        if(this.count) {
           peds.setListCount(new String(ch, start, length));
        }
   
    }

}


Here is my getter setter method code :
Code: Select all
public class ParsedDataSet {
    private String result = null;
    private String id=null;
    private String attr=null;
    private String maker=null;
    private String count=null;
   

    public String getResult() {
         return result;
    }
    public void setResult(String result) {
         this.result = result;
    }
    public void setId(String id){
       
       this.id=id;
       System.out.println("Id : " + this.id);
    }
   
    public String getAttr(){
       return attr;
    }
    public void setMaker(String maker){
       this.maker = maker;
       System.out.println("maker : " + this.maker);
    }
    public void setListCount(String count){
       this.count=count;
       
    }
       
   
    public String toString(){
       System.out.println("To string :" + this.maker);
       
       return this.maker;
                   
    }
}


And im getting the following output

System.out(): Id :
: INFO/System.out(): Id :
: INFO/System.out(): Id : AC
: INFO/System.out(): maker : AC
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id : 7
: INFO/System.out(): maker : 7
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): maker :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id : ACURA
: INFO/System.out(): maker : ACURA
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id : 32567
: INFO/System.out(): maker : 32567
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): maker :
: INFO/System.out(): maker :
: INFO/System.out(): To string :null

Please help me friends .


Hi, I think that your mistake in main class when you show the result, can you show the main class in here? some classes above are used to parse XML file.
Best 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 vaishumol » Thu Jul 01, 2010 4:37 pm

Thanks for your kind reply Alex. I fixed the issue. :-). Now its working perfectly. :-)
vaishumol
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Sun Jan 03, 2010 6:33 am

Re: Parsing XML from the Net - Using the SAXParser

Postby fwaokda » Wed Jul 07, 2010 9:04 pm

Has anyone created the getLast() function? I'm not having any luck... maybe I'm misunderstanding how it is supposed to work.
fwaokda
Freshman
Freshman
 
Posts: 8
Joined: Mon Feb 15, 2010 11:07 pm

Top

Re: Parsing XML from the Net - Using the SAXParser

Postby fwaokda » Thu Jul 08, 2010 2:28 am

well I figured that out but I'm getting a left bracket ( "[" ) and each string is separated by a comma... anyone know how i can get rid of these?

okay I've found that this problem is cause by the "toString()" function... so I'm working on iterating through them.

--------------------

Another question though is this xml file here (http://blog.nola.com/saints_impact/atom.xml) doesn't have strictly text in the <title></title>'s. Is there an easy way to get these titles in a proper string format? Thanks
fwaokda
Freshman
Freshman
 
Posts: 8
Joined: Mon Feb 15, 2010 11:07 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby kishore.thorata » Thu Jul 15, 2010 2:59 pm

Hi,

Can any one tell how to display images from tags like <image>http://someaddress.jpg</image>.
User avatar
kishore.thorata
Freshman
Freshman
 
Posts: 8
Joined: Wed Jun 02, 2010 7:21 am

Re: Parsing XML from the Net - Using the SAXParser

Postby opy001 » Fri Jul 16, 2010 9:31 pm

Just wanted to say thanks to plusminus (even though he doesnt come in here anymore lol) and also thanks to padde for keeping this thread going. I had to end up using XML Pull parser for my RSS but this thread helped me learn alot about the parsing itself among other things. Once I read this thread for the 10th time I managed to keep most of it in my head lol.

That brings up my next point. Guys, please dont just read the first post then ask plusminus to make it work for you. Try reading the entire thread, most of the answers are in here. Either that or use the search feature. I have to hand it to you padde, you got alot of patients lol.
opy001
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Sun Jul 11, 2010 5:24 am

Re: Parsing XML from the Net - Using the SAXParser

Postby fwaokda » Mon Jul 19, 2010 8:40 pm

I've found that my problem was/is when the characters() function is called and there is a &amp; in the title. It disregards all the stuff before and including the & and then displays the part after only. As soon as I figure this out I'll post a fix.

----------------------------------------- Update
So I solved the problem by moving the line: StringBuffer myStr = new StringBuffer();
out of characters() and then clear the buffer after each tag parsing. Hope that helps.
fwaokda
Freshman
Freshman
 
Posts: 8
Joined: Mon Feb 15, 2010 11:07 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby limteckchai » Tue Jul 20, 2010 8:13 am

Hi All,
Simple question here:
I tried the tutorial and it worked both my emulator and my motorola milestone (2.1).
I did my own XML stuff and it worked well on the emulator. But when i tested it on test phone, it says
Error:The connection was reset.
Anyone has a clue??


(Oh and having said that, i did do permissions afterall if i din the app wont work at all.)
Thanks in advancessssssssss
limteckchai
Experienced Developer
Experienced Developer
 
Posts: 92
Joined: Mon Jun 28, 2010 3:33 am

Re: Parsing XML from the Net - Using the SAXParser

Postby songotho » Wed Jul 21, 2010 8:26 am

limteckchai wrote:Hi All,
Simple question here:
I tried the tutorial and it worked both my emulator and my motorola milestone (2.1).
I did my own XML stuff and it worked well on the emulator. But when i tested it on test phone, it says
Error:The connection was reset.
Anyone has a clue??


(Oh and having said that, i did do permissions afterall if i din the app wont work at all.)
Thanks in advancessssssssss


Hi,
If you worked with emulator that is ok because ur emulator connected by ur computer. You need ensure ur phone have been connected. I think it will run well.
Best regards,
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 limteckchai » Wed Jul 21, 2010 8:48 am

Firstly, loads of thanks for the fast reply.

At the same time , i also asked my company's IT dudes. ( I am only an Intern, so my knowledge is limited)
Anyway it is because their information is protected and cannot be retrieved this way and can only be accessed thru a proxy. Does any have any example or links i can follow to read up more??

Thanks
limteckchai
Experienced Developer
Experienced Developer
 
Posts: 92
Joined: Mon Jun 28, 2010 3:33 am

Re: weird behavior of the function characters

Postby Amires » Wed Jul 21, 2010 10:53 pm

hey man ,
I am surprised to , i ran some tests on that function and i found out that sometimes the output is null sometimes it is the string we want to display but for some reason it just does not display it . so i dont really think the problem lies only on the fact that it is not inlined .

Damn, had the same problem. After changing to inline as you said, it works. It is weird imo, valid xml is valid xml.[/quote]
Amires
Freshman
Freshman
 
Posts: 5
Joined: Wed Jul 14, 2010 4:51 pm

Re: unknown error

Postby Amires » Wed Jul 21, 2010 10:55 pm

I think you have to add the permission in your manifest .
If that does not work just keep readinng people s posts and you will certainly find a solution to your problem :)

mel wrote:hey plusminus, I run your example and get this error of: "Error:unknown error" on the screen, any idea?

Edit...

the thing that is doing the error is this:

new InputSource(url.openStream())


this is on the ParsingXML.java

:S
Amires
Freshman
Freshman
 
Posts: 5
Joined: Wed Jul 14, 2010 4:51 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 11 guests