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 kevindorson » Tue May 25, 2010 11:21 am

HI,
can someone help me how to retrieve the attribute value using this kind of android sax xml parsing.

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


I'm able to retrieve the <mytag> attribute but unable to retrieve the sampleattribute="innertagAttribute" .
I'm using the parsing
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class AndroidSaxFeedParser extends BaseFeedParser {
  2.  
  3.     public AndroidSaxFeedParser(String feedUrl) {
  4.         super(feedUrl);
  5.     }
  6.  
  7.     public List<Message> parse() {
  8.         final Message currentMessage = new Message();
  9.         RootElement root = new RootElement("rss");
  10.         final List<Message> messages = new ArrayList<Message>();
  11.         Element channel = root.getChild("channel");
  12.         Element item = channel.getChild(ITEM);
  13.         item.setEndElementListener(new EndElementListener(){
  14.             public void end() {
  15.                 messages.add(currentMessage.copy());
  16.             }
  17.         });
  18.         item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
  19.             public void end(String body) {
  20.                 currentMessage.setTitle(body);
  21.             }
  22.         });
  23.         item.getChild(LINK).setEndTextElementListener(new EndTextElementListener(){
  24.             public void end(String body) {
  25.                 currentMessage.setLink(body);
  26.             }
  27.         });
  28.      
  29.         try {
  30.             Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,
  31. root.getContentHandler());
  32.         } catch (Exception e) {
  33.             throw new RuntimeException(e);
  34.         }
  35.         return messages;
  36.     }
  37. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
kevindorson
Developer
Developer
 
Posts: 44
Joined: Mon Mar 29, 2010 11:51 am

Top

Re: Parsing XML from the Net - Using the SAXParser

Postby kevindorson » Thu May 27, 2010 9:27 am

Is there someone out who can help me with the above query???
I'm still unable to find any support with regards to the same.
kevindorson
Developer
Developer
 
Posts: 44
Joined: Mon Mar 29, 2010 11:51 am

Re: Parsing XML from the Net - Using the SAXParser

Postby kevindorson » Thu May 27, 2010 9:27 am

Is there someone out who can help me with the above query???
I'm still unable to find any support with regards to the same.
kevindorson
Developer
Developer
 
Posts: 44
Joined: Mon Mar 29, 2010 11:51 am

Re: Parsing XML from the Net - Using the SAXParser

Postby cupid062985 » Tue Jun 01, 2010 7:02 am

Hi Guys,

Does anybody know how to escape HTML Entities using SAX? I have been reading forums about this and it seems there's still no solution for this one.

if i have an XML with a content below:
Code: Select all
Angels &amp; Demons


it will only return amp; Demons

Sir plusminus and other guys who know the solution, hope you can help me on my query.

Thanks! :D
cupid062985
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Jan 27, 2010 11:36 am

Re:

Postby svebee » Sat Jun 05, 2010 4:41 pm

padde wrote:XMLDemo.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.padde.xmldemo;
  2.  
  3. import java.util.Vector;
  4.  
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.widget.TextView;
  8.  
  9. public class XMLDemo extends Activity {
  10.     private Vector<Person> friends;
  11.        
  12.     public void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.main);
  15.        
  16.         friends = PersonParser.parse(getResources().openRawResource(R.raw.test));
  17.        
  18.         TextView tv = (TextView) findViewById(R.id.output);
  19.         for(Person p : friends) tv.append(p.toString()+"n");
  20.     }
  21. }
  22.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Person.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.padde.xmldemo;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.Vector;
  6.  
  7. import javax.xml.parsers.ParserConfigurationException;
  8. import javax.xml.parsers.SAXParser;
  9. import javax.xml.parsers.SAXParserFactory;
  10.  
  11. import org.xml.sax.Attributes;
  12. import org.xml.sax.InputSource;
  13. import org.xml.sax.SAXException;
  14. import org.xml.sax.XMLReader;
  15. import org.xml.sax.helpers.DefaultHandler;
  16.  
  17. public class Person {
  18.     private String name;
  19.     private String phone;
  20.    
  21.     public void setName(String n) { this.name = n; }
  22.     public void setPhone(String p) { this.phone = p; }
  23.     public String getName() { return this.name; }
  24.     public String getPhone() { return this.phone; }
  25.    
  26.     public String toString() {
  27.         return "Person[Name: "+this.name+", Phone: "+this.phone+"]";
  28.     }
  29. }
  30.  
  31. class PersonParser {
  32.     public static Vector<Person> parse(InputStream in) {
  33.         SAXParserFactory spf = SAXParserFactory.newInstance();
  34.         SAXParser sp;
  35.         try {
  36.             sp = spf.newSAXParser();
  37.             XMLReader xr = sp.getXMLReader();
  38.             PersonHandler ch = new PersonHandler();
  39.             xr.setContentHandler(ch);
  40.             xr.parse(new InputSource(in));
  41.             return ch.getParsedPersons();                
  42.         } catch (ParserConfigurationException e) {
  43.         } catch (SAXException e) {
  44.         } catch (IOException e) {}
  45.         return null;
  46.     }
  47. }
  48.  
  49. class PersonHandler extends DefaultHandler {
  50.     private enum TAGS { friends, person, name, phone }    
  51.     private StringBuffer tempString;
  52.     private Person tempPerson;
  53.     private Vector<Person> friends;
  54.    
  55.     public PersonHandler() {
  56.         super();
  57.         this.friends = new Vector<Person>();
  58.         this.tempString = new StringBuffer();
  59.     }
  60.    
  61.     public Vector<Person> getParsedPersons() { return friends; }
  62.  
  63.     public void startDocument() { }
  64.     public void endDocument() { }
  65.    
  66.     public void characters(char ch[], int start, int length) {
  67.         tempString.append(ch,start,length);
  68.     }
  69.    
  70.     public void startElement(String n, String l, String q, Attributes a) {
  71.         switch(TAGS.valueOf(l)) {
  72.             case person: tempPerson = new Person(); break;    
  73.             default: tempString.setLength(0); break;
  74.         }
  75.     }
  76.      
  77.     public void endElement(String n, String l, String q) {
  78.         switch(TAGS.valueOf(l)) {
  79.             case name: tempPerson.setName(tempString.toString()); break;
  80.             case phone: tempPerson.setPhone(tempString.toString()); break;        
  81.             case person: friends.add(tempPerson); break;
  82.             default: break;
  83.         }    
  84.     }
  85. }
  86.  
Parsed in 0.045 seconds, using GeSHi 1.0.8.4


res/layout/main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent">
  6.    
  7.     <TextView
  8.        android:id="@+id/output"  
  9.        android:layout_width="fill_parent"
  10.        android:layout_height="wrap_content"
  11.    />
  12. </LinearLayout>
  13.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


res/raw/test.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <friends>
  3.     <person>
  4.         <name>Jack</name>
  5.         <phone>111-111-111</phone>
  6.     </person>
  7.     <person>
  8.         <name>John</name>
  9.         <phone>222-222-222</phone>
  10.     </person>
  11. </friends>
  12.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Why "name" or "phone" can't be something with space between (for ex. "One car") or with foreign characters (for ex. "šđžćč"). Thanks :)

EDIT: For "space" - i removed from Person.java on line 74 code "tempString.setLength(0);" and now I can put spaces..but what about foreign characters or for example - $%&...? Tnx.
svebee
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Apr 21, 2010 10:19 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby wolwe » Mon Jun 07, 2010 7:46 pm

Hi, actually I perform a google product search and recieving the xml.
I'm wondering if there is no example source where somebody else does this?
Can anyone help me out, or am I entirely wrong?!
(This xml is huge and a challenge to be parsed in my opinion.)

I'm fighting with this problem for weeks now, so every help would be appreciated!
Thanks a lot!
wolwe
Freshman
Freshman
 
Posts: 5
Joined: Wed May 05, 2010 11:59 am
Location: Germany

Top

Re: Parsing XML from the Net - Using the SAXParser

Postby shadabansu » Tue Jun 08, 2010 6:57 pm

hi dude,

Please solve my problem. i have to parse a xml file which is present in SDcard and xml file which is present on hard disk.
please kindly provide me the solution. if you have any sample code please send me on my id

my email id is shadabansu@gmail.com
shadabansu
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jun 08, 2010 6:45 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby knightdominion » Thu Jun 10, 2010 12:51 pm

I am having an issue where trying to parse XML from one of my private google calendar pages is not returning the full string. For example, the full XML string that I want to parse is:
Code: Select all
<summary type='html'>When: Wed Jun 9, 2010 1pm to 2pm&amp;nbsp;
EDT&lt;br&gt;



&lt;br&gt;Event Status: confirmed</summary>


The string that is returned however, is just:
Code: Select all
Event Status: confirmed


Dont ask ME why the line breaks are in there or the html is in there, ask Google. I have tried using regular expressions to remove all special characters and line breaks from the string but still get the same result so to me, I understand that I am not getting the full string to begin with. Anyone ever seen this before or have any ideas on how to resolve this?
knightdominion
Freshman
Freshman
 
Posts: 2
Joined: Thu Jun 03, 2010 12:40 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby songotho » Fri Jun 11, 2010 7:16 am

knightdominion wrote:I am having an issue where trying to parse XML from one of my private google calendar pages is not returning the full string. For example, the full XML string that I want to parse is:
Code: Select all
<summary type='html'>When: Wed Jun 9, 2010 1pm to 2pm&amp;nbsp;
EDT&lt;br&gt;



&lt;br&gt;Event Status: confirmed</summary>


The string that is returned however, is just:
Code: Select all
Event Status: confirmed


Dont ask ME why the line breaks are in there or the html is in there, ask Google. I have tried using regular expressions to remove all special characters and line breaks from the string but still get the same result so to me, I understand that I am not getting the full string to begin with. Anyone ever seen this before or have any ideas on how to resolve this?


Hi men,

In fact, when you parse your xml
Code: Select all
<summary type='html'>When: Wed Jun 9, 2010 1pm to 2pm&amp;nbsp;
EDT&lt;br&gt;

&lt;br&gt;Event Status: confirmed</summary>
, exactly the result will be:
Code: Select all
When: Wed Jun 9, 2010 1pm to 2pm&amp;nbsp;
EDT&lt;br&gt;

&lt;br&gt;Event Status: confirmed
. This function will know the string in tag, it can not give you
Code: Select all
Event Status: confirmed
.

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 knightdominion » Fri Jun 11, 2010 12:32 pm

songotho wrote:
Hi men,

In fact, when you parse your xml
Code: Select all
<summary type='html'>When: Wed Jun 9, 2010 1pm to 2pm&amp;nbsp;
EDT&lt;br&gt;

&lt;br&gt;Event Status: confirmed</summary>
, exactly the result will be:
Code: Select all
When: Wed Jun 9, 2010 1pm to 2pm&amp;nbsp;
EDT&lt;br&gt;

&lt;br&gt;Event Status: confirmed
. This function will know the string in tag, it can not give you
Code: Select all
Event Status: confirmed
.

Best regard,
Alex.


Thanks for the response songotho. So your thinking that there is something wrong with the way I am printing the parsed line? I am saving the variable to a string and then printing that string. Is there a better way to do it? If it is parsing the entire string in the tag and returning it to the variable, then the only thing that that leaves to be broken in the process is printing the variable. Thanks.
knightdominion
Freshman
Freshman
 
Posts: 2
Joined: Thu Jun 03, 2010 12:40 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby anujmax » Sun Jun 20, 2010 6:59 pm

hi plusminus,
i ran the code but my emulator is giving an error:-
Error:Permission denied....
please help....
actually i want to make an RSS feed so i wanted to learn how to extract data from internet and then i would store it in database the retrieve for the user....is my approach right??
please help

anuj
anujmax
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Jun 20, 2010 6:54 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby xavi_madrid » Mon Jun 21, 2010 10:24 am

This is a good tutorial, thanks man
xavi_madrid
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jun 21, 2010 9:42 am

Re: Parsing XML from the Net - Using the SAXParser

Postby Jux » Tue Jun 22, 2010 9:25 am

What about overriding startPrefixMapping(String prefix, String uri)? When I do that, it never get's called.
Jux
Freshman
Freshman
 
Posts: 5
Joined: Mon May 31, 2010 1:58 pm

Re: Parsing XML from the Net - Using the SAXParser

Postby cupid062985 » Fri Jun 25, 2010 6:55 am

knightdominion wrote:
songotho wrote:
Hi men,

In fact, when you parse your xml
Code: Select all
<summary type='html'>When: Wed Jun 9, 2010 1pm to 2pm&amp;nbsp;
EDT&lt;br&gt;

&lt;br&gt;Event Status: confirmed</summary>
, exactly the result will be:
Code: Select all
When: Wed Jun 9, 2010 1pm to 2pm&amp;nbsp;
EDT&lt;br&gt;

&lt;br&gt;Event Status: confirmed
. This function will know the string in tag, it can not give you
Code: Select all
Event Status: confirmed
.

Best regard,
Alex.


Thanks for the response songotho. So your thinking that there is something wrong with the way I am printing the parsed line? I am saving the variable to a string and then printing that string. Is there a better way to do it? If it is parsing the entire string in the tag and returning it to the variable, then the only thing that that leaves to be broken in the process is printing the variable. Thanks.


Actually we have the same problem. It's a bug in SAX Parsing in which they cannot recognize HTML Entities. I hope somebody can help us.
cupid062985
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Jan 27, 2010 11:36 am

Re: Parsing XML from the Net - Using the SAXParser

Postby darko1002001 » Sat Jun 26, 2010 1:09 am

i need help using arraylist with the parser.

here is my code. the problem is that the list that is returned contains only the last xml part i need to parse and it repeats 10 times (thats the number of different values i have to get.)

my only guess is that it adds the pointer to the object for every element in the string array (maybe this info will help)

how do i fix it?

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <dsKurs>
  2.         <KursZbir>
  3.                 <Drzava>bla bla bla</Drzava>
  4.                 <DrzavaAng>bla bla bla</DrzavaAng>
  5.                 <NazivAng>bla bla bla</NazivAng>
  6.         </KursZbir>
  7. <KursZbir>
  8.                 <Drzava>bla bla bla</Drzava>
  9.                 <DrzavaAng>bla bla bla</DrzavaAng>
  10.                 <NazivAng>bla bla bla</NazivAng>
  11.         </KursZbir>
  12. <KursZbir>
  13.                 <Drzava>bla bla bla</Drzava>
  14.                 <DrzavaAng>bla bla bla</DrzavaAng>
  15.                 <NazivAng>bla bla bla</NazivAng>
  16.         </KursZbir>
  17. <KursZbir>
  18.                 <Drzava>bla bla bla</Drzava>
  19.                 <DrzavaAng>bla bla bla</DrzavaAng>
  20.                 <NazivAng>bla bla bla</NazivAng>
  21.         </KursZbir>
  22. </dsKurs>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


here is how i set it to textView on screen

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ArrayList<ParsedExampleDataSet> listaOdValuti =
  2.                                                            myExampleHandler.getParsedData();
  3.            
  4.            Integer i=listaOdValuti.size();
  5.            i.toString();
  6.            tv.setText(i+listaOdValuti.toString());
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package dveteam.nbrmconverter;
  2.  
  3.  
  4. import java.util.ArrayList;
  5.  
  6. import org.xml.sax.Attributes;
  7. import org.xml.sax.SAXException;
  8. import org.xml.sax.helpers.DefaultHandler;
  9.  
  10.  
  11. public class ExampleHandler extends DefaultHandler{
  12.  
  13.      // ===========================================================
  14.      // Fields
  15.      // ===========================================================
  16.      
  17.      private boolean in_outertag = false;
  18.      private boolean in_innertag = false;
  19.      private boolean in_Oznaka = false;
  20.      private boolean in_DrzavaAng= false;
  21.      private boolean in_Sreden= false;
  22.      
  23.      private ArrayList<ParsedExampleDataSet> list = new ArrayList<ParsedExampleDataSet>(22);
  24.      
  25.      private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
  26.      
  27.  
  28.      // ===========================================================
  29.      // Getter & Setter
  30.      // ===========================================================
  31.  
  32.      public ArrayList<ParsedExampleDataSet> getParsedData() {
  33.           return list;
  34.      }
  35.  
  36.      // ===========================================================
  37.      // Methods
  38.      // ===========================================================
  39.      @Override
  40.      public void startDocument() throws SAXException {
  41.           this.myParsedExampleDataSet = new ParsedExampleDataSet();
  42.      }
  43.  
  44.      @Override
  45.      public void endDocument() throws SAXException {
  46.           // Nothing to do
  47.      }
  48.  
  49.      /** Gets be called on opening tags like:
  50.       * <tag>
  51.       * Can provide attribute(s), when xml was like:
  52.       * <tag attribute="attributeValue">*/
  53.      @Override
  54.      public void startElement(String namespaceURI, String localName,
  55.                String qName, Attributes atts) throws SAXException {
  56.           if (localName.equals("dsKurs")) {
  57.                this.in_outertag = true;
  58.           }else if (localName.equals("KursZbir")) {
  59.                this.in_innertag = true;
  60.           }else if (localName.equals("Oznaka")) {
  61.                this.in_Oznaka = true;
  62.           } else if (localName.equals("Sreden")) {
  63.                this.in_Sreden = true;
  64.           }else if (localName.equals("DrzavaAng"))  {
  65.                   this.in_DrzavaAng= true;
  66.           }
  67.      }      
  68.          
  69.                  
  70.                   //else if (localName.equals("tagwithnumber")) {
  71.          // }
  72.                // Extract an Attribute
  73.               // String attrValue = atts.getValue("thenumber");
  74.               // int i = Integer.parseInt(attrValue);
  75.               // myParsedExampleDataSet.setExtractedInt(i);
  76.         //  }
  77.      
  78.      
  79.      /** Gets be called on closing tags like:
  80.       * </tag> */
  81.      @Override
  82.      public void endElement(String namespaceURI, String localName, String qName)
  83.                throws SAXException {
  84.          
  85.           if (localName.equals("dsKurs")) {
  86.                this.in_outertag = false;
  87.           }else if (localName.equals("KursZbir")) {
  88.                this.in_innertag = false;
  89.                
  90.                list.add(myParsedExampleDataSet);
  91.                
  92.           }else if (localName.equals("Oznaka")) {
  93.                this.in_Oznaka = false;
  94.           }else if (localName.equals("Sreden")) {
  95.                this.in_Sreden = false;
  96.           }else if (localName.equals("DrzavaAng"))  {
  97.                   this.in_DrzavaAng= false;
  98.           }
  99.      }
  100.      
  101.      /** Gets be called on the following structure:
  102.       * <tag>characters</tag> */
  103.      @Override
  104.     public void characters(char ch[], int start, int length) {
  105.           if(this.in_Oznaka){
  106.           myParsedExampleDataSet.setoznaka(new String(ch, start, length));
  107.           }
  108.           if(this.in_DrzavaAng){
  109.           myParsedExampleDataSet.setdrzavaang(new String(ch, start, length));
  110.           }
  111.           if(this.in_Sreden){
  112.               myParsedExampleDataSet.setsreden(new String(ch, start, length));
  113.               }
  114.          
  115.     }
  116. }
Parsed in 0.052 seconds, using GeSHi 1.0.8.4
darko1002001
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sat Jun 26, 2010 12:52 am

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests