Parsing XML from the Net - Using the SAXParser

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

Parsing XML from the Net - Using the SAXParser

Postby manish » Fri Feb 13, 2009 4:37 am

Hi mel,

I also got the same error but now I had solved the error.
You have to make an entry in ur androidmainfest.xml file

<uses-permission android:name="android.permission.INTERNET" />

In my case when i was trying the problem, I had placed the xml file in my local hardrive, had attached it to the IIS server and had given the path of localhost (we have to give the ip address of our pc. 127.0.0.1 or localhost will not work). I had done all these think because we access the net using proxy server and I have to set the path of the proxy server which I dont know where to set. If you are not using the proxy server no need to do that.
manish
Junior Developer
Junior Developer
 
Posts: 21
Joined: Thu Feb 05, 2009 1:06 pm

Top

Postby mel » Fri Feb 13, 2009 7:08 pm

hey manish!!! thx a lot!

it works now... but the string that says "anddev.org rulez =) " does not appear

hahaha... :S dont know why
mel
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Feb 12, 2009 6:53 pm

Postby EverYoung124 » Mon Feb 16, 2009 11:48 pm

tdi wrote:
rigeltrue wrote:
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
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <mytag>
  2. anddev.org rulez =)
  3. </mytag>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


to
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <mytag>anddev.org rulez =)</mytag>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


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.[/quote]

The same here. Need to change the xml file and upload to another place to test.

plusminus, could you please change the source xml file? Thanks for your tutorial.
Hang on.
EverYoung124
Freshman
Freshman
 
Posts: 2
Joined: Mon Dec 08, 2008 10:13 pm

Postby mel » Mon Feb 16, 2009 11:54 pm

thx EverYoung124 :)
mel
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Feb 12, 2009 6:53 pm

good tutorial on xml parsing

Postby sebastian » Fri Feb 20, 2009 12:08 pm

Thanks plusminus, I found this tutorial really helpful! 8)
sebastian
Freshman
Freshman
 
Posts: 6
Joined: Fri Feb 20, 2009 10:47 am

Postby Teikan » Fri Feb 20, 2009 8:18 pm

How would you extract the XML from inside the tag itself, ie this bit of code:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <pvp>
  2.  
  3. <lifetimehonorablekills value="6561"/>
  4.  
  5. <arenacurrency value="0"/>
  6.  
  7. </pvp>
  8.  
  9.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

from the WoW armory?

How would i extract the value 6561?
Most of the examples show a
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <tag>value</tag>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
setup

Edit:
NM, i just noticed that the tut actually includes an example, and i was just blind apparently.
Teikan
Freshman
Freshman
 
Posts: 9
Joined: Wed Jan 28, 2009 5:38 pm

Top

how can i extract the values present in mytag and myattr?

Postby viju85 » Wed Mar 04, 2009 8:51 am

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0"?>
  2.  
  3. <outertag>
  4.  
  5.      <innertag sampleattribute="innertagAttribute">
  6.  
  7.           <mytag myattr="sample_attribute">anddev.org rulez =)</mytag>
  8.  
  9.           <tagwithnumber thenumber="1337"/>
  10.  
  11.      </innertag>
  12.  
  13. </outertag>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


how can i extract mytag's value which is anddev.org rulez =)
and
myattr's value which is sample_attribute???

i tried using both characters() and startElement(). But somehow the parser does not seem to be going inside characters().
Please help.

Thanks,
Vijay.C
viju85
Junior Developer
Junior Developer
 
Posts: 20
Joined: Mon Feb 23, 2009 10:17 pm

Great tutorial!

Postby bonks » Sun Mar 08, 2009 10:34 am

Very understandable tutorial for android beginners. I come from a C#.NET background and the only android tutorials I've done so far are the hello world, notepad, and hello world views from Google. I jumped right into this one and was able to pull XML data from my favorite site in less than 2 hours by modifying the xml tag names.

I encountered a couple of challenges:

First I kept getting an "unknown error" exception whenever I get the url's stream. I solved this by adding the INTERNET permission in the manifest file.

Then after changing the XML source to my favorite site's rss feed, I was receiving an array of strings. When I appended all these strings to the TextView i noticed I could not scroll. I tried changing the single line attribute to false but that didn't seem to work. What I did instead was use a ListActivity and added each string to the item list. This allowed me to actually see all the data and scroll through it, which made me happy to see a successful application!

Thanks plusminus!
bonks
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Mar 08, 2009 10:14 am
Location: Phoenix, AZ

Postby SeraphimSerapis » Wed Mar 11, 2009 11:10 pm

Hello,
I wanted to thank you, too.
But I do really have a problem:
When I want to parse this XML File:

<marketexport>
<createtime_timestamp>1236801648</createtime_timestamp>
<createtime_date>11.03.09 - 21:00</createtime_date>
<ressources>
<ressource>
<name>Energie</name>
<price>14</price>
<number>11967033</number>
</ressource>
</ressources>
<race>
<race_name>Nova Federation</race_name>
<military_units>
<unit>
<name>NoF Marine</name>
<price>1200</price>
<number>845</number>
</unit>
</military_units>
<spy_units>
<unit>
<name>Thief</name>
<price>0</price>
<number>0</number>
</unit>
<unit>
<unit>
<name>Agent</name>
<price>0</price>
<number>0</number>
</unit>
</spy_units>
</race>
</marketexport>

With this code:

ExampleHandler:
Code: Select all
public void startElement(String namespaceURI, String localName,
          String qName, Attributes atts) throws SAXException {
       if (localName.equals("marktexport")) {
            this.in_marktexport = true;
        }
       if (localName.equals("ressources")) {
            this.in_ressources = true;
        }
       if (localName.equals("ressource")) {
            this.in_ressource = true;
        }
       if (localName.equals("race")) {
            this.in_race = true;
        }
       if (localName.equals("race_name")) {
          this.in_race_name = true;
        }
       if (localName.equals("military_units")) {
           this.in_military_units = true;
        }
       if (localName.equals("unit")) {
           this.in_unit = true;
        }
        if (localName.equals("name")) {   
           this.in_name = true;
        }
        if (localName.equals("price")) {
           this.in_price = true;
        }
        if (localName.equals("number")) {
           this.in_number = true;
        }
       
    }
   
    public void endElement(String namespaceURI, String localName, String qName)
              throws SAXException {
       if (localName.equals("marktexport")) {
            this.in_marktexport = false;
       }
       if (localName.equals("ressources")) {
            this.in_ressources = false;
       }
       if (localName.equals("ressource")) {
            this.in_ressource = false;
       }
       if (localName.equals("name")) {
           this.in_name = false;
       }
       if (localName.equals("price")) {
           this.in_price = false;
       }
       if (localName.equals("number")) {
           this.in_number = false;
       }
       if (localName.equals("race")) {
          this.in_race = false;
       }
       if (localName.equals("race_name")) {
          this.in_race_name = false;
       }
       if (localName.equals("military_units")) {
              this.in_military_units = false;
       }
       if (localName.equals("unit")) {
              this.in_unit = false;
       }
    }
   
    public void characters(char ch[], int start, int length) {
             if(this.in_race_name){
                myParsedExampleDataSet.setrace(new String(ch, start, length));
             }
               if(this.in_name){
                  myParsedExampleDataSet.setname(new String(ch, start, length));
                }
               if(this.in_price){
                   myParsedExampleDataSet.setprice(new String(ch, start, length));
                }
                if(this.in_number){
                   myParsedExampleDataSet.setnumber(new String(ch, start, length));
                }
    }


ParsedExampleDataSet:
Code: Select all
//Setter
    public void setprice(String price){
         this.price = price;
    }
    public void setname(String name) {
        this.name = name;
    }
    public void setnumber(String number) {
        this.number = number;
    }
    public void setrace(String race) {
        this.race = race;
    }
   
    //Getter
    public String getnumber() {
        return number;
    }
    public String getprice() {
        return price;
    }
    public String getname() {
        return name;
    }
    public String getrace() {
        return race;
    }
   
    //.toString()
    public String toString() {
        result = getrace()+"nName: "+getname()+"nPreis: "+getprice()+"nVerfügbare Anzahl: "+getnumber();
         return result;
    }


I Only get:
Nova Federation
Name: Agent
Preis: 0
Anzahl: 0

but i should get Thiefes, Guardians, Marines and so on, and the ressources too

Perhaps someone can help me with my problem :-)
SeraphimSerapis
Junior Developer
Junior Developer
 
Posts: 14
Joined: Wed Mar 11, 2009 10:57 pm

Postby Eddio » Thu Mar 19, 2009 1:00 am

How would you go about handling more than 1 copy of the same tag?
Eddio
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Mar 19, 2009 12:54 am

Postby Timmeah » Sat Mar 21, 2009 9:51 pm

This tutorial was very helpful! Thanks!

Is there also a tutorial or an easy way to this the another way around? So editing a *.txt on a webserver with your Android application?

Would be very helpful to me, because I want to create an online log-file of the actions which are being performed in the android-application.
Timmeah
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Feb 10, 2009 4:30 pm

Postby Neox » Sun Mar 22, 2009 1:29 pm

Thanks for the tutorial, but if in the xml tag there a value on more rows, this script take only last..

How do to take all rows?

es.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0"?>
  2.  
  3. <outertag>
  4.  
  5.      <innertag sampleattribute="innertagAttribute">
  6.  
  7.           <mytag>Provaaaaa prviamo sto perke non preche quello perche quello
  8.  
  9. prova 2 ovviamente
  10.  
  11. asdasdasd</mytag>
  12.  
  13.           <tagwithnumber thenumber="1327412"/>
  14.  
  15.      </innertag>
  16.  
  17. </outertag>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
Neox
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Mar 22, 2009 1:22 pm
Location: Italy

Postby joshhendo » Tue Mar 24, 2009 1:40 pm

Hi,

I am having problems with this when it comes to a tag with the character "&" in it.

If I have something like the following

Code: Select all
<searchResults>
  <result number="0">
    <name>Person D & J</name>
    <phone>555-555-555</phone>
  </result>
</searchResults>


Then it causes problems when parsing the <name> tag. I altered the script to return the value of the <name> tag into an array, and that will parse it into an array that looks like: ['Person D ', '&', ' J']

Unfortuantly, this cannot work the way I want it to with that bug in it. Im not sure what causes the bug, but I can't think of anything to help me out with this. Any code you need me to post, just ask, but it is basically what is in the tutorial added to parse that xml file.

Note: It works perfectly if there are no entries containing &.

Thanks, Josh
joshhendo
Freshman
Freshman
 
Posts: 4
Joined: Fri Mar 20, 2009 12:42 am
Location: Sydney, Australia

Postby androooooid » Sun Mar 29, 2009 12:06 pm

I am having an issue where it reads the characters fine in the emulator but on the real device, the characters are getting cut off... anyone else have this issue?
androooooid
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Sep 26, 2008 8:47 pm

A little clarification concerning the tutorial

Postby Jay666 » Mon Apr 06, 2009 10:44 pm

First of all, thanks a lot to the creator of the tutorial.

I know this tutorial is old but to people reading it now (like I just did since I'm currently learning android), a little clarification:

The characters method is not called only after the end tag of an element is reached. In fact, it may be called a number of times for each element content.

So in the tutorial where it says:

When the Parser reaches such a Tag, the following method gets called, providing the characters between the opening and the closing tag:
Java:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      /** Gets be called on the following structure:
  2.       * <tag>characters</tag> */
  3.      @Override
  4.     public void characters(char ch[], int start, int length) {
  5.           String textBetween = new String(ch, start, length);
  6.     }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


In reality if you want to be sure to get the entire content of an element you should not rely on characters to contain that content each time it's called. Instead, you should create a StringBuffer and append to it each time the characters method is called. Then you can reset the StringBuffer each time the startElement method is called.

This might be why some people were having problems with empty content when the content spanned multiple lines.
Jay666
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Apr 06, 2009 10:20 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests