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

  • Home
  • SEARCH
  • 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 7809473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:26:52+00:00 2026-06-02T03:26:52+00:00

I have a sax parser with an xml tag that contains the following text:

  • 0

I have a sax parser with an xml tag that contains the following text: “A & amp; B” (There’s no space there – added so it will not convert to & here)

It’s as though it’s getting converted twice and escaping due to ampersand with a result of “A”. Here’s the process:

Xml file is downloaded

InputStream _inputStream = _urlConnection.getInputStream();
                        BufferedInputStream _bufferedInputStream = new BufferedInputStream(_inputStream);
                        ByteArrayBuffer _byteArrayBuffer = new ByteArrayBuffer(64);

                        int current = 0;
                        while((current = _bufferedInputStream.read()) != -1)
                        {
                            _byteArrayBuffer.append((byte)current);
                        }

                        FileOutputStream _fileOutputStream = openFileOutput(_file, MODE_PRIVATE);

                        _fileOutputStream.write(_byteArrayBuffer.toByteArray());
                        _fileOutputStream.close();

Data is converted with Sax in the endElement

else if (inLocalName.equalsIgnoreCase(_nodeTitle))
        {
            _titleValue = currentValue;
            currentValue = "";
        }

In debug, the ampersand is already converted and the data truncated when I read it in my characters method in the handler.

I’ve seen a lot of questions about this but never a solution. Any ideas?

Thanks

Parser:

List<PropertiesList> _theList = null;

        try 
        {
            // Create Factory, Parser, Reader, Handler
            SAXParserFactory _saxParserFactory = SAXParserFactory.newInstance();
            SAXParser _saxParser = _saxParserFactory.newSAXParser();
            XMLReader _xmlReader = _saxParser.getXMLReader();
            HandlerReps _handler = new HandlerReps(inRegion, inAbbreviation);

            _xmlReader.setContentHandler(_handler);
            _xmlReader.parse(new InputSource(inStream));

            _theList = _handler.getTheList();
        } 

Handler:

// Called when Tag Begins
    @Override
    public void startElement(String uri, String inLocalName, String inQName, Attributes inAttributes) throws SAXException 
    {
        currentElement = false;
    }

    // Called when Tag Ends
    @Override
    public void endElement(String inUri, String inLocalName, String inQName) throws SAXException 
    {
        currentElement = false;

        // Title
        if (inLocalName.equalsIgnoreCase(_nodeValue))
        {
            if (_stateValue.equalsIgnoreCase(_abbreviation) && 
                _countryValue.equalsIgnoreCase(_region))
            {
                // Construct the object
                PropertiesRegion _regionObject = new PropertiesRegion(_titleValue, _address1Value);

                cList.add(_regionObject);

                Log.d(TAG, _regionObject.toString());
            }

            _titleValue = "";
            _address1Value = "";
        }

        // Title
        else if (inLocalName.equalsIgnoreCase(_nodeTitle))
        {
            _titleValue = currentValue;
            currentValue = "";
        }

        // Address1
        else if (inLocalName.equalsIgnoreCase(_nodeAddress1))
        {
            _address1Value = currentValue;
            currentValue = "";
        }
    }

    // Called to get Tag Characters
    @Override
    public void characters(char[] inChar, int inStart, int inLength) throws SAXException 
    {
        if (currentElement) 
        {
            currentValue = new String(inChar, inStart, inLength);
            currentElement = false;
        }
    }
  • 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-02T03:26:53+00:00Added an answer on June 2, 2026 at 3:26 am

    This is very likely the cause of your problem:

        if (currentElement) 
        {
            currentValue = new String(inChar, inStart, inLength);
            currentElement = false;
        }
    

    For each text content node, the SAX parser may send multiple characters() events to your handler. You only get the whole text if you concatenate all these events. But in your code, only the first of these events is used, because then you set currentElement = false.

    The problem is not ampersand conversion. As a general rule, when you describe a problem, it is often better to only describe the symptoms, not any supposed causes.

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

Sidebar

Related Questions

I have some XML that I'm parsing with a SAX parser in Java. It
I have student.xml file and am parsing this file using SAX Parser and now
I have a problem with using the SAX parser to parse a XML file.
I am using SAX to parse some XML. Let's say I have the following
I have a problem with sax parser and encoded text. I try to parse
I have to parse a XML structure in JAVA using the SAX parser. The
The problem again is that though i have succesfully implemented a SAX parser in
Possible Duplicate: JAVA SAX parser split calls to characters() I have an XML file
I have a problem with a SAX xml parser. I want to parse a
I have an error when parsing an xml document with sax parser in android.

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.