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 8477615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:31:01+00:00 2026-06-10T18:31:01+00:00

I have an Android app that parses XML using SAXParser. Everything goes ok, excepting

  • 0

I have an Android app that parses XML using SAXParser. Everything goes ok, excepting some texts that get duplicated and trimmed. For example: “Just do it, even if you do not know how!” becomes ” not know how!”

This is the DefaultHandler code. 10x!

DefaultHandler handler = new DefaultHandler()
    {
        Praise praise;
        String elementValue = null;
        Boolean elementOn = false;

        @Override
        public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException
        {

            if (localName.equals("praise"))
            {
                praise = new Praise();
                elementOn = true;
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException
        {

            // elementOn = false;

            if (localName.equals("PRAISE_TEXT"))
            {
                praise.setPraiseText(elementValue);
            }
            if (localName.equals("MOOD"))
            {
                praise.setMood(elementValue);
            }
            if (localName.equals("RATING"))
            {
                praise.setRating(Integer.valueOf(elementValue));
            }
            if (localName.equals("praise"))
            {
                elementOn = false;
                if (update)
                {
                    if (database.getPraiseByText(praise.getPraiseText(), db) == null)
                    {
                        database.addPraise(db, praise.getPraiseText(), praise.getMood(),
                            Integer.valueOf(praise.getRating()));
                    }
                }
                else
                    database.addPraise(db, praise.getPraiseText(), praise.getMood(),
                        Integer.valueOf(praise.getRating()));
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException
        {
            // StringBuffer b = new StringBuffer();
            if (elementOn)
            {
                elementValue = new String(ch, start, length);}}};
  • 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-06-10T18:31:02+00:00Added an answer on June 10, 2026 at 6:31 pm

    In SaxParsing, you do not have guarantee that characters will be called only once!

    For this, you should concatenate all the characters you receive withing the same element

    This is a just your code with a small modification. You should use StringBuilder instead of using String (:

    DefaultHandler handler = new DefaultHandler()
        {
            Praise praise;
            String elementValue = null;
            Boolean elementOn = false;
    
            @Override
            public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException
            {
                elementValue = new String();
    
                if (localName.equals("praise"))
                {
                    praise = new Praise();
                    elementOn = true;
                }
            }
    
            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException
            {
                if (localName.equals("PRAISE_TEXT"))
                {
                    praise.setPraiseText(elementValue);
                }
                if (localName.equals("MOOD"))
                {
                    praise.setMood(elementValue);
                }
                if (localName.equals("RATING"))
                {
                    praise.setRating(Integer.valueOf(elementValue));
                }
                if (localName.equals("praise"))
                {
                    elementOn = false;
                    if (update)
                    {
                        if (database.getPraiseByText(praise.getPraiseText(), db) == null)
                        {
                            database.addPraise(db, praise.getPraiseText(), praise.getMood(),
                                Integer.valueOf(praise.getRating()));
                        }
                    }
                    else
                        database.addPraise(db, praise.getPraiseText(), praise.getMood(),
                            Integer.valueOf(praise.getRating()));
                }
            }
    
            @Override
            public void characters(char[] ch, int start, int length) throws SAXException
            {
                // StringBuffer b = new StringBuffer();
                if (elementOn)
                {
                    elementValue = elementValue + new String(ch, start, length);
                }
            }
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Android client app that communicates with server using Socket . On my
I'm building an Android app that parses a pretty big XML file (around 500
I have a working android app using TextView, some formatting (line breaks, rows of
So I'm writing an android app, and I have an xml parser class that
I have an android app that I am trying to launch and it gives
I have an Android app that has an Application class and I have another
I have an android app that plays audio from the application class. I have
I have an Android app that I had working a few months ago which
I have an Android app that begins by displaying a dialog. When I set
I started developing an android app that have to interact with MMS attachements, in

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.