Parsing XML from the Net - Using the SAXParser

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

Postby arams » Sat Aug 29, 2009 10:27 am

From Net which XML file u going to parsing
like:
images/tut/basic/parsingxml/example.xml

if u click the above link u got a xml file .

but in urs:
http://rss.cnn.com/rss/edition_world.rss
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Top

Postby jyoti » Sat Aug 29, 2009 10:55 am

arams wrote:From Net which XML file u going to parsing
like:
images/tut/basic/parsingxml/example.xml

if u click the above link u got a xml file .

but in urs:
http://rss.cnn.com/rss/edition_world.rss





this is the RSSFeeding
this link shows as a html page
but actually this is a xml page
right click on this page(http://rss.cnn.com/rss/edition_world.rss)
and click to view source

that is an xml file which i have to parse
jyoti
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Aug 25, 2009 1:39 pm

Problems with Parsing

Postby Chri » Mon Aug 31, 2009 12:18 pm

Hi!

Thanks for your beautiful tutorial!

But now i have 2 important questions!!

1. I coppied the code but it doesnt work really well!
the number is shown but the string does not!
I debugged it and saw that there are 3 runs when the string is readed:
the first run show me /n/t
the second /t/t/t anddev rules =)
and the third /n

and so only /n is desplayed!!!

2. I would like to parse a string instead of an url! How can i do that?

please help! it´s very urgent!!!
Chri
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Mon Aug 31, 2009 12:08 pm

Re: Same problem like dariusz

Postby Chri » Tue Sep 01, 2009 8:43 am

jhandal7 wrote:[b] bstubbs wrote:
Artur79, did you ever figure out how to solve the missing string?

I'm getting the same results:


Quote:

ExtractedString =
ExtractedInt = 1337



Anyone else getting this, found a solution? Thanks


Hi
I meet the same proplem.

Chnage the xml file from
XML:
<mytag>
anddev.org rulez =)
</mytag>


to
XML:
<mytag>anddev.org rulez =)</mytag>


That works.


Damn, had the same problem. After changing to inline as you said, it works. It is weird imo, valid xml is valid xml.
_________________
---
Dariusz Dwornikowski


[u]I am using SDK 1.0,this code need upgrade?

:D
thanks


How you can read from a .xml File instead of an URL?
I also would like to know, how to parse a String which is recived from a server?

mfg Chri
Chri
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Mon Aug 31, 2009 12:08 pm

Rally good edvice

Postby Chri » Tue Sep 01, 2009 9:25 am

xinwei wrote:hi,i find there is a problem with the following method:

/** Gets be called on the following structure:
* <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {
if(this.in_mytag){
myParsedExampleDataSet.setExtractedString(new String(ch, start, length));
}
}

that: you should define a StringBuffer to obtain data from <tag>characters</tag>,for the characters may contain some blanks, when i try this demo, i got a null with "ExtractedString" .

first define a private varible :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. StringBuffer str = new StringBuffer();
  2. then,replace myParsedExampleDataSet.setExtractedString(new String(ch, start, length));
  3. with
  4.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

str.append(new String(ch,start,length));
myParsedExampleDataSet.setExtractedString(str.toString());

and in the class :ParsedExampleDataSet
[/syntax]
Code: Select all
    public String toString(){
         return "ExtractedString = " + this.extractedString
                   + "nExtractedInt = " + this.extractedInt;
    }

should change to
[syntax="java"]
Code: Select all
    public String toString(){
         return "ExtractedString = " + this.extractedString.trim()
                   + "nExtractedInt = " + this.extractedInt;
    }


That´s a really good advice and it works perfect!!!

Thank´s for that
Chri
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Mon Aug 31, 2009 12:08 pm

Another Question

Postby Chri » Tue Sep 01, 2009 11:42 am

I have a lot of questions today! :)

my next question is, how to read an integer instead of an string in the case <id>2345</id>

which code i have to write at the void characters funktion?

mfg Chri
Chri
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Mon Aug 31, 2009 12:08 pm

Top

I need help again

Postby Chri » Wed Sep 02, 2009 2:51 pm

plusminus wrote:Hello Anthony,

is pretty long ago... so your xml looks like this (simplified):
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <!-- 21 times the same structure... -->
  2. <frbny:Key>
  3.      <frbny:CURR>AUD</frbny:CURR>
  4. </frbny:Key>
  5. <frbny:Obs OBS_STATUS="A" OBS_CONF="F">
  6.      <frbny:OBS_VALUE>0.9069</frbny:OBS_VALUE>
  7. </frbny:Obs>
  8. <!-- ... "loop" -->
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


right :?:

So i would do it like this:
  1. When entering [font=Lucida Console]<frbny:CURR>[/font] add one new element to the output-list
  2. When in [font=Lucida Console]characters()[/font] and [font=Lucida Console]this.in_curr_tag == true[/font], get the last element from the list (which is the one just added) and set its String.
  3. When entering [font=Lucida Console]<frbny:OBS_VALUE>[/font], do nothing, as the most recent element in the list is waiting to get the Double filled.
  4. When in [font=Lucida Console]characters()[/font] and [font=Lucida Console]this.in_curr_tag == true[/font], fill the Double of the last added Peds in the list.

So, without any testing:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.util.ArrayList;
  2.  
  3. import org.xml.sax.Attributes;
  4. import org.xml.sax.SAXException;
  5. import org.xml.sax.helpers.DefaultHandler;
  6.  
  7. public class ExampleHandler extends DefaultHandler {
  8.    
  9.      // only the relevant tags
  10.      
  11.      private boolean in_curr_tag = false;
  12.      private boolean in_obs_tag = false;
  13.      
  14.      private ArrayList<ParsedExampleDataSet> list = new ArrayList<ParsedExampleDataSet>(22); // 22 == capacity
  15.        
  16.      public ArrayList<ParsedExampleDataSet> getParsedData(){
  17.           return this.list;
  18.      }
  19.      
  20.      @Override
  21.      public void startDocument() throws SAXException {
  22.           //nothing to do here
  23.      }
  24.      
  25.      @Override
  26.      public void endDocument() throws SAXException {
  27.           //nothing to do here
  28.      }
  29.      
  30.      /*
  31.       * Gets be called on opening tags like
  32.       * <tag>
  33.       * Can provide attribute(s), when xml was like:
  34.       * <tag attribute="attributeValue">*/
  35.       @Override
  36.       public void startElement(String namespaceURI, String localName,
  37.                                         String qName, Attributes atts)throws SAXException{
  38.          
  39.            if(localName.equals("frbny:CURR")){
  40.                 list.add(new ParsedExampelDataSet());
  41.                 this.in_curr_tag = true;
  42.            }else if(localName.equals("frbny:OBS_VALUE")){
  43.                 this.in_obs_tag = true;
  44.            }
  45.       }
  46.      
  47.       //Called on closing tags like </tag>
  48.       @Override
  49.       public void endElement(String namespaceURI, String localName, String qName) throws SAXException{
  50.            if(localName.equals("frbny:CURR")){
  51.                 this.in_curr_tag = false;
  52.            }else if(localName.equals("frbny:OBS_VALUE")){
  53.                 this.in_obs_tag = false;
  54.            }
  55.       }
  56.      
  57.       /** Gets be called on the following structure:
  58.       * <tag>characters</tag> */
  59.      @Override
  60.     public void characters(char ch[], int start, int length) {
  61.      // getLast()-Function is probably not existing, need to do on your own
  62.       if(this.in_curr_tag){  
  63.            list.getLast().setExtractedString(new String(ch, start, length));
  64.       }
  65.       if(this.in_obs_tag){
  66.             list.getLast().setExtractedDouble(Double.parseDouble(new String(ch, start, length)));
  67.       }
  68.     }
  69. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Regards,
plusminus


How i put my result in an intent?
I am statring ParsingXML as an Activity with Result and in the file ParsingXML.java I want to return the catched ArrayList<ParsedEcampleDataSet> with onActivityResult.
But now i have to put my ArrayList as an extra in an intent!

I think the code should look like:

Intent i = new Intent();
Bundle b = new Bundle();
b.putStringArrayList(key, value)
i.putExtras(b)

How i do this? I rally need help!!!
Chri
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Mon Aug 31, 2009 12:08 pm

Postby dr3as » Thu Sep 03, 2009 10:11 am

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <outertag>
  2.  
  3. &#8722;
  4.  
  5. <innertag sampleattribute="innertagAttribute">
  6.  
  7. <mytag>anddev.org rulez =)</mytag>
  8.  
  9. <tagwithnumber thenumber="1337"/>
  10.  
  11. </innertag>
  12.  
  13. &#8722;
  14.  
  15. <innertag sampleattribute="innertagAttribute2">
  16.  
  17. <mytag>anddev.org rulez =)2</mytag>
  18.  
  19. <tagwithnumber thenumber="13372"/>
  20.  
  21. </innertag>
  22.  
  23. </outertag>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

I use this as my test xml, and want not only the last mytag and tagwithnumber to come up, what should I change in the example code to fix this?
dr3as
Freshman
Freshman
 
Posts: 5
Joined: Thu Sep 03, 2009 9:55 am

Postby Chri » Thu Sep 03, 2009 10:19 am

dr3as wrote:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <outertag>
  2. <innertag sampleattribute="innertagAttribute">
  3. <mytag>anddev.org rulez =)</mytag>
  4. <tagwithnumber thenumber="1337"/>
  5. </innertag>
  6. <innertag sampleattribute="innertagAttribute2">
  7. <mytag>anddev.org rulez =)2</mytag>
  8. <tagwithnumber thenumber="13372"/>
  9. </innertag>
  10. </outertag>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

I use this as my test xml, and want not only the last mytag and tagwithnumber to come up, what should I change in the example code to fix this?


Hi!

Look at the example obove your comment!!! :wink:
You havet to make a ArrayList!
Chri
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Mon Aug 31, 2009 12:08 pm

Postby dr3as » Thu Sep 03, 2009 10:45 am

I kind of looked in to that, but didn't get it to work. So i hoped someone had a example made for the examplecode in the first post :P

I guess i have to test more ;)
dr3as
Freshman
Freshman
 
Posts: 5
Joined: Thu Sep 03, 2009 9:55 am

your right

Postby Chri » Thu Sep 03, 2009 11:42 am

Try, Try, Try!!! :)

Im also hanging on a hugh problem! I all i can do, is to try :wink:
But learning by duing!
Chri
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Mon Aug 31, 2009 12:08 pm

Postby vik » Wed Sep 09, 2009 7:37 am

m tryin to do the first parsing example as given but some how extractedstring shows no value... though extractedint does show...

so can anyone help me out as to what is the problem!!!
User avatar
vik
Senior Developer
Senior Developer
 
Posts: 141
Joined: Wed Sep 09, 2009 7:32 am

Answere

Postby Chri » Wed Sep 09, 2009 8:08 am

Hi vik!

Look up to the post: Posted: Tue Sep 01, 2009 9:25 am Post subject: Rally good edvice

There is a description how you buffer your String and then it will work!

But be careful! If you have a .xml file with the format: <example>text</example>
Then it´s better to delete the buffer again!
The buffer in this example only works with the format: <example>
text
</example>


wkr Chri
Chri
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Mon Aug 31, 2009 12:08 pm

Postby vik » Wed Sep 09, 2009 8:31 am

yea thanx 4 the help chri... working nw..
User avatar
vik
Senior Developer
Senior Developer
 
Posts: 141
Joined: Wed Sep 09, 2009 7:32 am

Postby Fluckysan » Wed Sep 23, 2009 6:09 pm

Hi,
first I would to thank PlusMinus for this tuto :)

It work just fine but I have a strange behaviors when the handler parse strings with comma or dot
I'm not sure if the comma or the dot is the problem but this is always the same kind of data ===> Google coordinates like this one :
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <text>43.691987,7.245054</text>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


The handler seems to cut the string !
Sometimes I just get "43.691987", sometimes "43.691987,"

Please help me :/
Fluckysan
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Jul 01, 2009 7:57 am

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests