Parsing XML from the Net - Using the SAXParser

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

Postby galip91 » Mon Jan 11, 2010 1:46 pm

Hi i'm combining different webservices to my app and i've come across one that isn't workig.

The ones that ARE working are like this:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2.  
  3. <HuizenZoekerWebServices version="1" date="2008-10-28">
  4.  
  5.         <Responce>
  6.  
  7.               <name>blabla</name>
  8.  
  9.         </Response>
  10.  
  11. </HuizenZoekerWebServices>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


But now i have one that has 'xmlns' or something in it's XML-code. My regular XML parser can't seem to parse it.

This is the new webservice that DOESN'T work:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <DataSet xmlns="http://pathe.nl/filmfeed/">
  4.  
  5. <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
  6.  
  7.     <NewDataSet xmlns="">
  8.  
  9.       <agenda diffgr:id="agenda1" msdata:rowOrder="0">
  10.  
  11.         <agendaid>2363092</agendaid>
  12.  
  13.         <bios_id>6</bios_id>
  14.  
  15.         <film_id>845</film_id>
  16.  
  17.         <tijdstip>2010-01-12T21:00:00+01:00</tijdstip>
  18.  
  19.         <dag>2010-01-12T00:00:00+01:00</dag>
  20.  
  21.         <special>4</special>
  22.  
  23.         <ind_vip>N</ind_vip>
  24.  
  25.         <zaalnummer>7</zaalnummer>
  26.  
  27.         <type>S</type>
  28.  
  29.       </agenda>
  30.  
  31. </NewDataSet>
  32.  
  33. </diffgr:diffgram>
  34.  
  35. </DataSet>
  36.  
  37. </DataSet>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


This is my code

The main activity; MessageList.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MessageList extends ListActivity{
  2.  
  3.        
  4.  
  5.         private List<Waardes> waardes;
  6.  
  7.         int a = 0;
  8.  
  9.         private String TAG = "Galip";
  10.  
  11.        
  12.  
  13.     @Override
  14.  
  15.     public void onCreate(Bundle icicle) {
  16.  
  17.         super.onCreate(icicle);
  18.  
  19.         setContentView(R.layout.main);
  20.  
  21.         loadFeed();
  22.  
  23.     }
  24.  
  25.        
  26.  
  27.         private void loadFeed(){
  28.  
  29.         try{
  30.  
  31.                
  32.  
  33.                 Parser parser = new Parser();
  34.  
  35.                 waardes = parser.parse("PTH", 51.9596, 4.5688);
  36.  
  37.                 List<String> titles = new ArrayList<String>();
  38.  
  39.                 for (Waardes w : waardes){
  40.  
  41.                         titles.add("agendaID: " + w.getPTHagendaid());
  42.  
  43.                         titles.add("biosID: " + w.getPTHbiosid());
  44.  
  45.                         titles.add("FilmID: " + w.getPTHfilmid());
  46.  
  47.                         titles.add("Afstand: " + w.getINGAfstand() + " meter");
  48.  
  49.                         titles.add(" ");
  50.  
  51.                 }              
  52.  
  53.                 ArrayAdapter<String> adapter =
  54.  
  55.                         new ArrayAdapter<String>(this, R.layout.row,titles);
  56.  
  57.                 this.setListAdapter(adapter);
  58.  
  59.         } catch (Throwable t){
  60.  
  61.                 Log.e("AndroidNews",t.getMessage(),t);
  62.  
  63.         }
  64.  
  65.         }
  66.  
  67. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


The parser method in Parser.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public List<Waardes> parsePTH(double d, double e2) {
  2.  
  3.                 final Waardes currentMessage = new Waardes();
  4.  
  5.                 final List<Waardes> waardes = new ArrayList<Waardes>();
  6.  
  7.                 final String url = "http://pathe.nl/filmfeed/";
  8.  
  9.                 RootElement root = new RootElement(url, "DataSet");
  10.  
  11.                 Element diffgram = root.getChild("diffgr:diffgram");
  12.  
  13.                 Element NewDataSet = diffgram.getChild("NewDataSet");
  14.  
  15.                 Element agenda = NewDataSet.getChild("agenda");
  16.  
  17.                
  18.  
  19.                 agenda.setEndElementListener(new EndElementListener(){
  20.  
  21.                         public void end() {
  22.  
  23.                                 waardes.add(currentMessage.PTHcopy());
  24.  
  25.                         }
  26.  
  27.                 });
  28.  
  29.                 agenda.getChild("agendaid").setEndTextElementListener(new EndTextElementListener(){
  30.  
  31.                         public void end(String body){
  32.  
  33.                                 currentMessage.setPTHagendaid(body);
  34.  
  35.                         }
  36.  
  37.                        
  38.  
  39.                 });
  40.  
  41.                 agenda.getChild("bios_id").setEndTextElementListener(new EndTextElementListener(){
  42.  
  43.                         public void end(String body) {
  44.  
  45.                                 currentMessage.setPTHbiosid(body);
  46.  
  47.                         }
  48.  
  49.                 });
  50.  
  51.                 agenda.getChild("film_id").setEndTextElementListener(new EndTextElementListener(){
  52.  
  53.                         public void end(String body) {
  54.  
  55.                                 currentMessage.setPTHfilmid(body);
  56.  
  57.                         }
  58.  
  59.                 });
  60.  
  61.                 agenda.getChild("tijdstip").setEndTextElementListener(new EndTextElementListener(){
  62.  
  63.                         public void end(String body) {
  64.  
  65.                                 currentMessage.setPTHtijdstip(body);
  66.  
  67.                         }
  68.  
  69.                 });
  70.  
  71.                 agenda.getChild("dag").setEndTextElementListener(new EndTextElementListener(){
  72.  
  73.                         public void end(String body) {
  74.  
  75.                                 currentMessage.setPTHdag(body);
  76.  
  77.                         }
  78.  
  79.                 });
  80.  
  81.                 agenda.getChild("special").setEndTextElementListener(new EndTextElementListener(){
  82.  
  83.                         public void end(String body) {
  84.  
  85.                                 currentMessage.setPTHspecial(body);
  86.  
  87.                         }
  88.  
  89.                 });
  90.  
  91.                 agenda.getChild("ind_vip").setEndTextElementListener(new EndTextElementListener(){
  92.  
  93.                         public void end(String body) {
  94.  
  95.                                 currentMessage.setPTHindvip(body);
  96.  
  97.                         }
  98.  
  99.                 });
  100.  
  101.                 agenda.getChild("zaalnummer").setEndTextElementListener(new EndTextElementListener(){
  102.  
  103.                         public void end(String body) {
  104.  
  105.                                 currentMessage.setPTHzaalnr(body);
  106.  
  107.                         }
  108.  
  109.                 });
  110.  
  111.                 agenda.getChild("type").setEndTextElementListener(new EndTextElementListener(){
  112.  
  113.                         public void end(String body) {
  114.  
  115.                                 currentMessage.setPTHtype(body);
  116.  
  117.                         }
  118.  
  119.                 });
  120.  
  121.                 String feedUrlString = "http://www.pathe.nl/export/export.asmx/HaalAgenda?filmId=0&biosId=6";
  122.  
  123.                 URL feedUrl;
  124.  
  125.                 try {
  126.  
  127.                         feedUrl = new URL(feedUrlString);
  128.  
  129.                         Xml.parse(feedUrl.openConnection().getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
  130.  
  131.                 } catch (Exception e) {
  132.  
  133.                         throw new RuntimeException(e);
  134.  
  135.                 }
  136.  
  137.                 return waardes;
  138.  
  139.         }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


Waardes.java (where the values get saved. with setters and getters)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //Pathe
  2.  
  3.         private String PTHdiffgr;
  4.  
  5.         private String PTHagendaid;
  6.  
  7.         private String PTHbiosid;
  8.  
  9.         private String PTHfilmid;
  10.  
  11.         private String PTHtijdstip;
  12.  
  13.         private String PTHdag;
  14.  
  15.         private String PTHspecial;
  16.  
  17.         private String PTHindvip;
  18.  
  19.         private String PTHzaalnr;
  20.  
  21.         private String PTHtype;
  22.  
  23.  
  24.  
  25. //Pathe getters and setters (not all of them)
  26.  
  27.         public String getPTHagendaid() {
  28.  
  29.                 return PTHagendaid;
  30.  
  31.         }
  32.  
  33.  
  34.  
  35.         public void setPTHagendaid(String pTHagendaid) {
  36.  
  37.                 this.PTHagendaid = pTHagendaid;
  38.  
  39.         }
  40.  
  41.  
  42.  
  43.         public String getPTHbiosid() {
  44.  
  45.                 return PTHbiosid;
  46.  
  47.         }
  48.  
  49.  
  50.  
  51.         public void setPTHbiosid(String pTHbiosid) {
  52.  
  53.                 this.PTHbiosid = pTHbiosid;
  54.  
  55.         }
  56.  
  57.        
  58.  
  59. public Waardes PTHcopy(){
  60.  
  61.                 Waardes copy = new Waardes();
  62.  
  63.                 copy.PTHdiffgr = PTHdiffgr;
  64.  
  65.                 copy.PTHagendaid = PTHagendaid;
  66.  
  67.                 copy.PTHbiosid = PTHbiosid;
  68.  
  69.                 copy.PTHfilmid = PTHfilmid;
  70.  
  71.                 copy.PTHtijdstip = PTHtijdstip;
  72.  
  73.                 copy.PTHdag = PTHdag;
  74.  
  75.                 copy.PTHspecial = PTHspecial;
  76.  
  77.                 copy.PTHindvip = PTHindvip;
  78.  
  79.                 copy.PTHzaalnr = PTHzaalnr;
  80.  
  81.                 copy.PTHtype = PTHtype;
  82.  
  83.                 return copy;
  84.  
  85.         }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


More information and explanations about the service can be found at http://www.pathe.nl/export/export.asmx?op=HaalAgenda
Here it shows the different ways of getting the data (Soap 1.1/1.2, HTTP GET etc.) Maybe that can help you.

I hope someone can help me.

If you need more information, just ask.
galip91
Freshman
Freshman
 
Posts: 5
Joined: Mon Jan 11, 2010 1:13 pm

Top

Postby ice8 » Thu Jan 14, 2010 10:00 am

Hi, I have this error to override the startElement method:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      @Override
  2.  
  3.      public void startElement(String namespaceURI, String localName,
  4.  
  5.                  String qName, Attributes atts) throws SAXException {
  6.  
  7.           if (localName.equals("BusinessList")) {
  8.  
  9.                this.in_businesslist = true;
  10.  
  11.           }else if (localName.equals("business")) {
  12.  
  13.                this.in_business = true;
  14.  
  15.           }else if (localName.equals("name")) {
  16.  
  17.                this.in_name = true;
  18.  
  19.           }else if (localName.equals("address")) {
  20.  
  21.               this.in_address = true;
  22.  
  23.           }else if (localName.equals("phone")) {
  24.  
  25.               this.in_phone = true;
  26.  
  27.           }
  28.  
  29.      }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


The error is:
The method startElement(String, String, String, Attributes) of type BusinessHandler must override a superclass method

The methods endElement, startDocument, etc. are overrided fine.

What happen? Help please. Thanks!!
ice8
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Jan 14, 2010 9:57 am

Postby galip91 » Sat Jan 16, 2010 7:08 pm

C'mon someone has to know to the answer to my problem :(

I really need to know, I have a deadline this thursday and I've tried everything :'(
galip91
Freshman
Freshman
 
Posts: 5
Joined: Mon Jan 11, 2010 1:13 pm

Must we parse?

Postby JohnRock » Sun Jan 17, 2010 6:04 pm

Hey - thanks for the great tutorial! I am new to the droid and wanted to know - is it possible to consume xml on the android, from a webservice, and have the xml de-serialized straight into a list of pojos without having to manually parse each element yourself? In other words, if my webservice is taking a list of Message objects and transforming that into xml, what is the best practices way to consume that xml on the android and reproduce the same list of Message objects (complex types)?

I have read your tutorial, and also seen an example using XmlPullParser ... I am still unclear what the best practices way of doing this should be. Thanks for your help.
JohnRock
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Jan 17, 2010 5:49 pm

Postby hamldreza » Fri Jan 22, 2010 5:14 pm

I have two questions:

1- When I change the XML code (e.g. change the 1337 to 1400) and run the application again, but it does not change.

2- How can I insert the menu that without exiting the application data can be refreshed?

Thankss
hamldreza
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Jan 06, 2010 4:31 am

Unable to get "mytag" value

Postby maneesh_17 » Fri Jan 22, 2010 11:00 pm

Hi,

I used the above procedure but I am unable to get the value of "mytag" on screen ?

Please help me out.I am attaching the code.

Thanks,
Manish
Attachments
ParsingXML.zip
(33.62 KiB) Downloaded 114 times
maneesh_17
Junior Developer
Junior Developer
 
Posts: 17
Joined: Tue Sep 08, 2009 12:27 am
Location: Gurgaon,India

Top

Re: Parsing XML from the Net - Using the SAXParser

Postby pradeep_android » Thu Feb 04, 2010 8:54 pm

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
pradeep_android
Freshman
Freshman
 
Posts: 7
Joined: Thu Feb 04, 2010 5:19 am

Working XML Parsing code

Postby maneesh_17 » Thu Feb 04, 2010 10:16 pm

Hi,

The Attached code of XML parsing is working fine.You can deploy it.

Thanks,
Manish
Attachments
ParsingXML_C.zip
(32.9 KiB) Downloaded 194 times
maneesh_17
Junior Developer
Junior Developer
 
Posts: 17
Joined: Tue Sep 08, 2009 12:27 am
Location: Gurgaon,India

Postby kameltreiber84 » Fri Feb 05, 2010 10:09 am

Hi!
Thanks for that awesome tutorial.

Is there a way to save the parsed file into a Document, for example to handle it with jdom?
kameltreiber84
Junior Developer
Junior Developer
 
Posts: 18
Joined: Tue Jan 26, 2010 10:24 pm

Re: Working XML Parsing code

Postby pradeep_android » Fri Feb 05, 2010 11:23 am

maneesh_17 wrote:Hi,

The Attached code of XML parsing is working fine.You can deploy it.

Thanks,
Manish


i tried ur coding.. the error says "the operation timeout outparsingxml"
pradeep_android
Freshman
Freshman
 
Posts: 7
Joined: Thu Feb 04, 2010 5:19 am

parse xml problems

Postby toobsco42 » Sun Feb 07, 2010 2:45 am

hello plusminus. i tried to follow your example and i loaded all three of your files into a new project and i am getting the wrong output i get:

ExtractedString =
ExtractedInt = 1337

it doesnt look like it prints out the extracted string so then i changed the toString method to print out the length of the string and it prints that the length is 2. i am wondering why this might be. i changed the permission in the manifest.xml file to allow for internet. what could be the problem?
toobsco42
Freshman
Freshman
 
Posts: 4
Joined: Sun Feb 07, 2010 2:19 am

Postby Aikidoka » Tue Feb 09, 2010 12:31 am

I just ran into a not well-formed/invalid token error. I was reading XML off the net that was iso-8859-1 and not utf-8. This can be handled by declaring your InputSource and then setting the encoding before parsing.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         InputSource in = new InputSource(url.openStream());
  3.  
  4.         // Force encoding type as it is not utf-8
  5.  
  6.         in.setEncoding("iso-8859-1");
  7.  
  8.         xr.parse(in);
  9.  
  10.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Now I don't see in this tutorial how I can handle repeating nodes and their children. I'm guessing there is another one around here I haven't found yet for that? Basically the stream I'm reading are XML representations of objects. I'm getting the stream and need to convert it back to objects. I've read I can use xstream for this.
Aikidoka
Freshman
Freshman
 
Posts: 2
Joined: Tue Feb 09, 2010 12:20 am
Location: Baltimore, MD

Postby rahulsak » Wed Feb 10, 2010 7:26 am

Hello,
my xml file is like this...
wen i run my code it display only one Date , one time n one theater name
plz tell me hw to display other info?

<?xml version="1.0"?>
<movies>
<movie name="abc">
<showtimes>
<theater name="Cinema 1">
<day date="21.3.10">
<time>11.00</time>
<time>21.30</time>
</day>
<day date="29.3.10">
<time>18.30</time>
<time>20.30</time>
</day>
</theater>
<theater name="Cinema 2">
<day date="27.3.10">
</day>
</theater>
</showtimes>
</movie>
</movies>
rahulsak
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 10, 2010 7:16 am

Postby Aikidoka » Wed Feb 10, 2010 3:48 pm

rahulsak wrote:Hello,
my xml file is like this...
wen i run my code it display only one Date , one time n one theater name
plz tell me hw to display other info?


Just saw how to do something similar myself. Do you have a theater object? At the beginning of your handler declare a theatre object (you'll have to code that). Also create a Vector to add objects to. In the start element method, declare a new object and add it to the vector when you are hitting a "theater" tag. The theater object will still point to the vector element so you can alter it. Now just add all the data as necessary for the other tags. Each time you hit a theater tag, the theater object will point to a new instance. Therefore you will not over write a previous instance. Now you can create a method to return the vector, of just pull the data out and send it as text.

It looks like your theater object will need the following:
String name
an object to represent a day with times or variables for the day and times and a way to associate them. I imaging a struct could handle this ( I don't do structs myself )?

I'm actually coding something like this myself. Multiple objects in an XML file.
Aikidoka
Freshman
Freshman
 
Posts: 2
Joined: Tue Feb 09, 2010 12:20 am
Location: Baltimore, MD

to layout

Postby my7h1c » Wed Feb 24, 2010 9:42 am

Is it possible to move the text to an xml layout
my7h1c
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Feb 24, 2010 9:38 am

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests