Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6372343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:13:32+00:00 2026-05-25T01:13:32+00:00

I have been trying to use the SAX parser on Android to display some

  • 0

I have been trying to use the SAX parser on Android to display some xml that is the result of a query to a DB.

I get the right answer from the DB, but when I want to parse the xml and dump the content into a Text view, the ExampleHandler (ParsedExampleDataSet) returns an Object with an empty string. I have been trying to trace where the problem is but it eludes me.

So far I know the data is parsed correctly in the characters method, I managed to print the content out to the Log file, but I cannot return the string at that point.

I am sure more experimented programmers could fix this very easily:

My xml file is pretty basic, <body> blablablah </body>.

Here are the files:

 package com.ignacio.BilingualSTT;

 import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 import android.util.Log;
 import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;  
 import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.entity.UrlEncodedFormEntity;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.ByteArrayBuffer;
 import org.xml.sax.InputSource;
 import org.xml.sax.XMLReader;
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.TextView;
 import android.widget.Toast;

public class PostAndGet extends Activity {

    private static String Utterance = null;
    TextView Title;
    TextView Content;
    private static final String TAG = "TextToSpeechDemo";
    String text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.en_results);
            Title = (TextView)findViewById(R.id.A1_title);
            Content= (TextView) findViewById(R.id.A1_content);
            text = "";

            Bundle b = this.getIntent().getExtras();
            if (b !=null){
                    Utterance = b.getString("query");
            }
            postData(Utterance);
    }

    /**
 * Handle the action of the Back button
 */

public void sendback(View v)
{
    Intent Sendback=new Intent(this, EnglishRecognizer.class);
            startActivity(Sendback);
}

/**
 * Start HTTP Post call
 */

public void postData(String Utterance){
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://hcc.cs.clemson.edu/~ignm/ignacio/Android_QueryResults.php");  

            try {
                    // Add data to the HTTP Request data has to be named query since the PHP page in the server is expecting that name to run the query on the DB
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
                    nameValuePairs.add(new BasicNameValuePair("query", Utterance));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request and retrieve answer from PHP side.
                    HttpResponse response = httpclient.execute(httppost);
                    InputStream is = response.getEntity().getContent();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    ByteArrayBuffer baf = new ByteArrayBuffer(20);
                    int current = 0;

                    while((current = bis.read()) != -1){
                            baf.append((byte)current);
                    }  

                    // Convert the Bytes read to a String and display it in the Title Container
                    text = new String(baf.toByteArray());
                    Title.setText(text);

                    //Send Text to
                    parseXML (text);


            } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(this, "Error in the connection", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(this,"IO Exception error", Toast.LENGTH_SHORT).show();
            }
    }


public void parseXML (String text){

    String AnswerExt = text;

    try {
            /* Create a URL we want to load some xml-data from. */
            URL url = new URL("http://hcc.cs.clemson.edu/~ignm/ignacio/"+AnswerExt.toString());

            Log.i(TAG, url.toString());

            /* Get a SAXParser from the SAXPArserFactory. */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            /* Get the XMLReader of the SAXParser we created. */
            XMLReader xr = sp.getXMLReader();
            /* Create a new ContentHandler and apply it to the XML-Reader*/
            ExampleHandler myExampleHandler = new ExampleHandler();
            xr.setContentHandler(myExampleHandler);

            /* Parse the xml-data from our URL. */
            xr.parse(new InputSource(url.openStream()));
            /* Parsing has finished. */

            /* Our ExampleHandler now provides the parsed data to us. */
            ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();


            /* Set the result to be displayed in our GUI. */
            Content.setText(parsedExampleDataSet.getExtractedString());


    } catch (Exception e) {
             /* Display any Error to the GUI. */
             Content.setText("Error: " + e.getMessage());
             Log.e(TAG, "QueryError", e);
    }
}
}

Example Handler modified to fit the parsing of my xml.

 package com.ignacio.BilingualSTT;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler; 
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;

public class ExampleHandler extends DefaultHandler{

    // ===========================================================
    // Fields
    // ===========================================================

    private boolean bodytag = false;
    private static final String TAG = "TextToSpeechDemo";

    public ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
    public String myReturnString = null;

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    public ParsedExampleDataSet getParsedData() {  
            return this.myParsedExampleDataSet;
    }

    // ===========================================================
    // Methods
    // ===========================================================
    @Override
    public void startDocument() throws SAXException {
            this.myParsedExampleDataSet = new ParsedExampleDataSet();
    }

    @Override
    public void endDocument() throws SAXException {
            // Nothing to do
    }

    /** Gets be called on opening tags like:
     * <tag>
     * Can provide attribute(s), when xml was like:
     * <tag attribute="attributeValue">*/
    @Override
    public void startElement(String namespaceURI, String localName,
                    String qName, Attributes atts) throws SAXException {
            if (localName.equals("body")) {
                    this.bodytag = true;
            }
    }

    /** Gets be called on closing tags like:
     * </tag> */
    @Override
    public void endElement(String namespaceURI, String localName, String qName)
                    throws SAXException {
            if (localName.equals("body")) {
                    this.bodytag = false;
            }
    }

    /** Gets be called on the following structure:
     * <tag>characters</tag> */
    @Override
public void characters(char ch[], int start, int length) {
            if(this.bodytag){
            myParsedExampleDataSet.setExtractedString(new String(ch, start, length));
            //String Temp = myParsedExampleDataSet.getExtractedString();
            //Log.i(TAG, Temp);      
    }
    }

 }

ParsedExampleDataSet:

 package com.ignacio.BilingualSTT;

  public class ParsedExampleDataSet {
    private String extractedString = "No content yet";

    public String getExtractedString() {
            return extractedString;
    }
    public void setExtractedString(String extractedString) {
            this.extractedString = extractedString;
    }      
    public String toString(){
            return this.extractedString;
    }
 }

Thank you so much!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:13:33+00:00Added an answer on May 25, 2026 at 1:13 am

    The characters method can get called multiple times while inside a tag, especially if the element value contains whitespace.

    You might be able to see this happening in that Logcat call by multiple lines being printed.

    From the documentation:

    The Parser will call this method to report each chunk of character
    data. SAX parsers may return all contiguous character data in a single
    chunk, or they may split it into several chunks; however, all of the
    characters in any single event must come from the same external entity
    so that the Locator provides useful information.

    Rather than using setExtractedString, you should append to the string that’s being built up, possibly using a StringBuilder.

    See the accepted answer here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to use a formula that is used to work out
I have been trying use the edit_post_link() function to contain an image. All of
I have been trying to use the Perl utility/module "prove" as a test harness
I have been trying to use the latest NAnt (0.86 Beta 1) for a
I have been trying to use Server Side Includes on my website to save
I have been trying to use declarative data bound ASP.NET, and struggling with the
I have been trying to use a simple jQuery operation to dynamically match and
I have been trying to use routes.rb for creating a URL /similar-to-:product (where product
I have been trying to use an ajax-style file upload component (like the dojox
I'm using CoreData (with SQLite as the store) in an iOS 4 iPhone application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.