SAX Parser Problem in Android

Put your problem here if it does not fit any of the other categories.

SAX Parser Problem in Android

Postby nisha » Fri Mar 28, 2008 1:29 pm

Hi all,


I am using SAX parser to parse the values from xml... I am facing some problem... I am able to get the values of attributes in the xml.... however control is not goin in endElement() and characters() methods... can anybody help me out in this....


Regards,
nisha
nisha
Experienced Developer
Experienced Developer
 
Posts: 80
Joined: Fri Feb 15, 2008 7:52 am

Top

Postby Jesmiatka » Fri Mar 28, 2008 1:56 pm

Can you show some code and the layout of the XML?
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby plusminus » Fri Mar 28, 2008 2:07 pm

Hello nisha,

maybe have a look here: :arrow: http://www.anddev.org/viewtopic.php?t=353

If it doesn't help you, follow Jesmiatka advice :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby nisha » Mon Mar 31, 2008 6:57 am

Hi ,
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.aztec.ParsingXML;
  2.  
  3.  
  4.  
  5. import java.util.Stack;
  6.  
  7.  
  8.  
  9. import org.xml.sax.Attributes;
  10.  
  11. import org.xml.sax.SAXException;
  12.  
  13. import org.xml.sax.helpers.DefaultHandler;
  14.  
  15.  
  16.  
  17. import android.util.Log;
  18.  
  19.  
  20.  
  21. public class ExampleHandler extends DefaultHandler {
  22.  
  23.        
  24.  
  25. //       ===========================================================
  26.  
  27.     // Fields
  28.  
  29.     // ===========================================================
  30.  
  31.      
  32.  
  33.  
  34.  
  35.         private Stack stack = new Stack();
  36.  
  37.      
  38.  
  39.     private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
  40.  
  41.  
  42.  
  43.     // ===========================================================
  44.  
  45.     // Getter & Setter
  46.  
  47.     // ===========================================================
  48.  
  49.  
  50.  
  51.     public ParsedExampleDataSet getParsedData() {
  52.  
  53.          return this.myParsedExampleDataSet;
  54.  
  55.     }
  56.  
  57.  
  58.  
  59.     // ===========================================================
  60.  
  61.     // Methods
  62.  
  63.     // ===========================================================
  64.  
  65.     @Override
  66.  
  67.     public void characters(char ch[], int start, int length) {
  68.  
  69.         /*  if(this.in_mytag){
  70.  
  71.           myParsedExampleDataSet.setExtractedString(new String(ch, start, length));
  72.  
  73.        
  74.  
  75.      } */
  76.  
  77.  
  78.  
  79. // this is not getting printed.... <img src="http://www.anddev.org/images/smilies/sad.png" alt=":(" title="Sad" />
  80.  
  81.  
  82.  
  83.         Log.e("chars are=================++++++++++++++++++++","");
  84.  
  85.        
  86.  
  87.         String chars = new String(ch,start, length).trim();
  88.  
  89.         Log.e("chars are=================",chars);
  90.  
  91.         if(chars.length() >0)
  92.  
  93.         {
  94.  
  95.                 String elName = (String) stack.peek();
  96.  
  97.                 Log.e("top of the stack is", elName);
  98.  
  99.                
  100.  
  101.                 if(elName.equals("price"))
  102.  
  103.                 {
  104.  
  105.                         String price = chars;
  106.  
  107.                         Log.e("price is********", elName);
  108.  
  109.                 }
  110.  
  111.         }
  112.  
  113.     }
  114.  
  115.  
  116.  
  117.      
  118.  
  119.     @Override
  120.  
  121.     public void startDocument() throws SAXException {
  122.  
  123.          this.myParsedExampleDataSet = new ParsedExampleDataSet();
  124.  
  125.     }
  126.  
  127.  
  128.  
  129.     @Override
  130.  
  131.     public void endDocument() throws SAXException {
  132.  
  133.          // Nothing to do
  134.  
  135.     }
  136.  
  137.  
  138.  
  139.     @Override
  140.  
  141.     public void startElement(String namespaceURI, String localName,
  142.  
  143.               String qName, Attributes atts) throws SAXException {
  144.  
  145.          if (localName.equals("prices")) {
  146.  
  147.                  String timestamp = atts.getValue("timestamp");
  148.  
  149.           //    this.in_outertag = true;
  150.  
  151.                  Log.e("the timestamp is", timestamp);
  152.  
  153.                    }
  154.  
  155.                 else if (localName.equals("tagwithnumber")) {
  156.  
  157.               // Extract an Attribute
  158.  
  159.               String attrValue = atts.getValue("thenumber");
  160.  
  161.               int i = Integer.parseInt(attrValue);
  162.  
  163.               myParsedExampleDataSet.setExtractedInt(i);
  164.  
  165.          }
  166.  
  167.          stack.push(localName);
  168.  
  169.     }
  170.  
  171.      
  172.  
  173.     /** Gets be called on closing tags like:
  174.  
  175.      * </tag> */
  176.  
  177.     @Override
  178.  
  179.     public void endElement(String namespaceURI, String localName, String qName)
  180.  
  181.               throws SAXException {
  182.  
  183.         namespaceURI = null;
  184.  
  185. // this is not getting printed <img src="http://www.anddev.org/images/smilies/sad.png" alt=":(" title="Sad" /> ,control is not comin here...
  186.  
  187.         Log.e("endElement ===========","");
  188.  
  189.         stack.pop();
  190.  
  191.     }
  192.  
  193.      
  194.  
  195.     /** Gets be called on the following structure:
  196.  
  197.      * <tag>characters</tag> */
  198.  
  199.    
  200.  
  201. }
  202.  
  203.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4



Control is not goin inside characters() and endElement() methods.... because of that i cant read element value if i have... i am getting all attribute values... is there any way to make control go inside those methods??? Plz reply...

Regards,
nisha
nisha
Experienced Developer
Experienced Developer
 
Posts: 80
Joined: Fri Feb 15, 2008 7:52 am

Postby Jesmiatka » Mon Mar 31, 2008 8:08 am

Hi,

I once used some code for parsing a SOAP message.
Some elements contained a " : " hence why the split is in.
The elements in my document that I needed were "out" and "echoMethodResponse".

You might try and check it out.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class echoMethodHandler extends DefaultHandler {
  2.  
  3.  
  4.  
  5.     ArrayList array = new ArrayList();
  6.  
  7.     String current = null;
  8.  
  9.     private boolean isOut = false;
  10.  
  11.  
  12.  
  13.     public void startElement(String namespaceURI,
  14.  
  15.                              String localName,
  16.  
  17.                              String qName,
  18.  
  19.                              Attributes atts) throws SAXException {
  20.  
  21.         String elementName;
  22.  
  23.        
  24.  
  25.         if(qName.contains(":")){
  26.  
  27.                 elementName = qName.split(":")[1];
  28.  
  29.         } else {
  30.  
  31.                 elementName = qName;
  32.  
  33.         }
  34.  
  35.                
  36.  
  37.         if (elementName.equals("echoMethodResponse")) {
  38.  
  39.             current = new String();
  40.  
  41.         } else if (elementName.equals("out")) {
  42.  
  43.             isOut = true;
  44.  
  45.         }
  46.  
  47.     }
  48.  
  49.  
  50.  
  51.     public void endElement(String namespaceURI,
  52.  
  53.                            String localName,
  54.  
  55.                            String qName) throws SAXException {
  56.  
  57.         String elementName;
  58.  
  59.        
  60.  
  61.         if(qName.contains(":")){
  62.  
  63.                 elementName = qName.split(":")[1];
  64.  
  65.         } else {
  66.  
  67.                 elementName = qName;
  68.  
  69.         }
  70.  
  71.        
  72.  
  73.         if (elementName.equals("out")) {
  74.  
  75.                 isOut = false;
  76.  
  77.         } else if (elementName.equals("echoMethodResponse")) {
  78.  
  79.             array.add(current);
  80.  
  81.             current = null;
  82.  
  83.         }
  84.  
  85.     }
  86.  
  87.  
  88.  
  89.     public void characters(char[] chars, int i, int i1) throws SAXException {
  90.  
  91.         if (isOut) {
  92.  
  93.             current = new String(chars, i, i1);
  94.  
  95.         }
  96.  
  97.     }
  98.  
  99.  
  100.  
  101.     public List getLocations() {
  102.  
  103.         return array;
  104.  
  105.     }
  106.  
  107. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby nisha » Mon Mar 31, 2008 11:08 am

Hi ,


i got attribute values by parsing the XML file... the XML file which i am using is having attributes and values for it but elements are not having value... So i feel because elements are not having value, control is not goin inside character() method..... Hey thanks for the code...


Regards,
nisha
nisha
Experienced Developer
Experienced Developer
 
Posts: 80
Joined: Fri Feb 15, 2008 7:52 am

Top

How to read properties of a tag using SAXParser ?

Postby Tinky » Sat Nov 22, 2008 9:39 am

hi

I am using SAXParser for parsing response coming from youtube site of topratedvideo's, but the response is given in the form of a property of a tag as shown below..

<media:content url="rtsp://rtsp2.youtube.com/CioLENy73wIaIQk4KunEORhwnhMYESARFEgGUg5nZGF0YV9zdGFuZGFyZAw=/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="1301" yt:format="6"/>

i need to extract the properties (like url) from the tag.

In dataHandler characters() function gets called only when data presents in between start and end tag like shown below
<tag>Data</tag>

Can anyone tell me how to parse url from above xml code using SAXParser or do i need to use another parser.. like feedParser ?

Thanks in advance
Tinky
Tinky
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 09, 2008 9:18 am
Location: India

Postby richard.puckett » Sun Nov 23, 2008 11:52 pm

The startElement has this signature:

public void startElement(String uri, String localName, String qName, Attributes atts)

What you're looking for is called an "attribute", so you'll want to parse the Attributes parameter to pull out the ones you're interested in. You'll probably be interested in the "getValue" method.

Docs are here:

http://code.google.com/android/referenc ... butes.html
Image
richard.puckett
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Dec 27, 2007 2:37 am
Location: Seattle, WA

Postby Tinky » Mon Nov 24, 2008 2:53 pm

thanks.. i m able to get those values now :)
Tinky
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 09, 2008 9:18 am
Location: India

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 17 guests