Parsing XML from the Net - Using the SAXParser

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

Postby elendryl » Thu Jul 30, 2009 10:37 am

my mistake in my copy/paste but i have had this in my code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void characters(char ch[], int start, int length) {
  2.  
  3.         if(this.in_id){
  4.  
  5.         myParsedDataSet.setId(new String(ch, start, length));
  6.  
  7.         }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


but same problem
elendryl
Freshman
Freshman
 
Posts: 9
Joined: Thu Jul 30, 2009 9:43 am

Top

Postby padde » Thu Jul 30, 2009 12:32 pm

Ah.. okay.. i will check that later.. at least if eclipse and the android SDK is win7 ready.. i report back asap :)
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby elendryl » Thu Jul 30, 2009 12:45 pm

thanks to you

i'll continue to investigate my code :D
elendryl
Freshman
Freshman
 
Posts: 9
Joined: Thu Jul 30, 2009 9:43 am

Postby elendryl » Mon Aug 03, 2009 7:33 am

Need more help please

problems still here :(
elendryl
Freshman
Freshman
 
Posts: 9
Joined: Thu Jul 30, 2009 9:43 am

Postby Silent_Noise » Tue Aug 04, 2009 2:11 am

Hi, everybody. I done everything the same as tutorial (thanks to you), my code is the same. But it returned only the last record from my XML...
Here my XML-file:
<?xml version="1.0" encoding="utf-8" ?>
- <dataset>
- <row>
<fio_creator>Buka</fio_creator>
<task_exp>08-Aug-2009</task_exp>
<task_desc>shlak</task_desc>
<state>0</state>
<task_id>2</task_id>
</row>
- <row>
<fio_creator>Buka</fio_creator>
<task_exp>30-Aug-2009</task_exp>
<task_desc>save_the_future</task_desc>
<state>0</state>
<task_id>1</task_id>
</row>
</dataset>
Can anyone help me?
(sorry for my English :))
Silent_Noise
Freshman
Freshman
 
Posts: 3
Joined: Fri Jul 31, 2009 5:54 am
Location: Russia

Postby elendryl » Tue Aug 04, 2009 8:20 am

In order to keep all "record" you need in your setter to keep all your record

example :


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    public void setId(String Id) {
  2.  
  3.      
  4.  
  5.                 this.Id = Id;
  6.  
  7.     }
  8.  
  9. public String toString(){
  10.  
  11.          
  12.  
  13.         return this.Id;
  14.  
  15.          //return "toto";      
  16.  
  17.     }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

With that you will keep only the last record, because at each iteration the next record will erase the previous

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    public void setId(String Id) {
  2.  
  3.         if (this.Id == null)
  4.  
  5.         {
  6.  
  7.                 this.Id = Id;
  8.  
  9.         }
  10.  
  11.         else {
  12.  
  13.                 this.Id += Id;
  14.  
  15.         }
  16.  
  17.     }
  18.  
  19. public String toString(){
  20.  
  21.          
  22.  
  23.         return this.Id;
  24.  
  25.          //return "toto";      
  26.  
  27.     }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

Here the next record is add to the previous in a same string
you can use an array if you want and add each new record at a different index.

I could not be very clear in my explanation like my english is not very good too :D
elendryl
Freshman
Freshman
 
Posts: 9
Joined: Thu Jul 30, 2009 9:43 am

Top

Postby Silent_Noise » Wed Aug 05, 2009 2:39 am

elendryl, thank you :) it works... I'll think about using array.
Silent_Noise
Freshman
Freshman
 
Posts: 3
Joined: Fri Jul 31, 2009 5:54 am
Location: Russia

Postby selim » Wed Aug 12, 2009 11:23 pm

Firstly, thank u 4 valuable info...
2ndly, what if i want to validate the xml file against a schema and then parse the data...
3rdly, if the xml code is written in an EditText not in a file, how i can parse it instead of using the url way...
deep thx...
selim
Freshman
Freshman
 
Posts: 5
Joined: Wed Aug 12, 2009 4:46 pm

Postby jyoti » Tue Aug 25, 2009 1:51 pm

plusminus wrote:Hello Anthony,

I would do it that way:
Additionally create a (Array)List of ResultSets. And every time the parser reaches this tag :darrow: , add a new ParsedExampleDataSet to the List:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. </frbny:Obs>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Regards,
plusminus


hello plusminus

i am new to android
as u said to create a (arraylist) list of results.
i donn't understand how should i do that.
plz clearify that

thanks in advance
jyoti
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Aug 25, 2009 1:39 pm

Postby selim » Tue Aug 25, 2009 2:18 pm

I got the answer for my qs...
but is there any validation schema within SAX parser??
selim
Freshman
Freshman
 
Posts: 5
Joined: Wed Aug 12, 2009 4:46 pm

Postby xinwei » Thu Aug 27, 2009 12:18 pm

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.035 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;
    }
xinwei
Freshman
Freshman
 
Posts: 2
Joined: Thu Aug 27, 2009 11:26 am

Postby xinwei » Thu Aug 27, 2009 12:18 pm

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.035 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;
    }
xinwei
Freshman
Freshman
 
Posts: 2
Joined: Thu Aug 27, 2009 11:26 am

Postby jyoti » Fri Aug 28, 2009 11:10 am

hello all

i am trying to parse an xml file from net
n want to show them in list

but its not working

plzzzz check itttt

its important :cry:
thanku in advance
Attachments
rssfeedingmy.zip
(47.32 KiB) Downloaded 135 times
jyoti
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Aug 25, 2009 1:39 pm

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

jyoti wrote:hello all

i am trying to parse an xml file from net
n want to show them in list

but its not working

plzzzz check itttt

its important :cry:
thanku in advance


This is your xml link
http://rss.cnn.com/rss/edition_world.rss
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

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






actually problem is this
when i run my application it shows
"No RSS Available"

i debugged my application.
but after the<copyright> tag of xml file.
it throws an exception.
don't know why
jyoti
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Aug 25, 2009 1:39 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests