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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:21:30+00:00 2026-05-23T23:21:30+00:00

My free webhost appends analytics javascript to all PHP and HTML files. Which is

  • 0

My free webhost appends analytics javascript to all PHP and HTML files. Which is fine, except that I want to send XML to my Android app, and it’s invalidating my files.

Since XML is parsed in its entirety (and blows up) before passed along to my SAX ContentHandler, I can’t just catch the exception and continue merrily along with a fleshed out object. (Which I tried, and then felt sheepish about.)

Any suggestions on a reasonably efficient strategy?

I’m about to create a class that will take my InputStream, read through it until I find the junk, break, then take what I just wrote to, convert it back into an InputStream and pass it along like nothing happened. But I’m worried that it’ll be grossly inefficient, have bugs I shouldn’t have to deal with (e.g. breaking on binary values such as embedded images) and hopefully unnecessary.

FWIW, this is part of an Android project, so I’m using the android.util.Xml class (see source code). When I traced the exception, it took me to a native appendChars function that is itself being called from a network of private methods anyway, so subclassing anything seems to be unreasonably useless.

Here’s the salient bit from my stacktrace:

E/AndroidRuntime(  678): Caused by: org.apache.harmony.xml.ExpatParser$ParseException: At line 3, column 0: junk after document element
E/AndroidRuntime(  678):    at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:523)
E/AndroidRuntime(  678):    at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:482)
E/AndroidRuntime(  678):    at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:320)
E/AndroidRuntime(  678):    at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:277)

I guess in the end I’m asking for opinions on whether the InputStream -> manually parse to OutputStream -> recreate InputStream -> pass along solution is as horrible as I think it is.

  • 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-23T23:21:32+00:00Added an answer on May 23, 2026 at 11:21 pm

    I’m about to create a class that will take my InputStream, read
    through it until I find the junk, break, then take what I just wrote
    to, convert it back into an InputStream and pass it along like nothing
    happened. But I’m worried that it’ll be grossly inefficient, have bugs
    I shouldn’t have to deal with (e.g. breaking on binary values such as
    embedded images) and hopefully unnecessary.

    you could use a FilterStream for that no need for a buffer

    best thing to do is add a delimiter to the end of the XML like --theXML ends HERE -- or a char not found in XML like a group of 16 \u04 chars (you then only need to check every 16th byte) to the end of the XML and read until you find it

    implementation assuming \u04 delim

    class WebStream extends FilterInputStream {
    
        byte[] buff = new byte[1024];
        int offset = 0, length = 0;
    
        public WebStream(InputStream i) {
            super(i);
        }
    
        @Override
        public boolean markSupported() {
            return false;
        }
    
        @Override
        public int read() throws IOException {
            if (offset == length)
                readNextChunk();
            if (length == -1)
                return -1;// eof
            return buff[offset++];
        }
    
        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            if (offset == length)
                readNextChunk();
            if (length == -1)
                return -1;// eof
    
            int cop = length - offset;
            if (len < cop)
                cop = len;
            System.arraycopy(buff, offset, b, off, cop);
            offset += cop;
            return cop;
        }
    
        private void readNextChunk() throws IOException {
            if (offset <= length) {
                System.arraycopy(buff, offset, buff, 0, length - offset);
                length -= offset;
                offset = 0;
            }
            int read = in.read(buff, length, buff.length - length);
            if (read < 0 && length <= 0) {
                length = -1;
                offset = 0;
                return;
            }
    
            // note that this is assuming ascii compatible
            // anything like utf16 or utf32 will break here
            for (int i = length; i < read + length; i += 16) {
                if (buff[i] == 0x04) {
                    while (buff[--i] == 0x04)
                        ;// find beginning of delim block
                    length = i;
                    read = 0;
                }
            }
        }
    
    }
    

    note this misses throws, some error checking and needs proper debugging

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

Sidebar

Related Questions

My free app (which is not on the market yet) has a button that
Is there any free or commercial component written in .NET (no COM interop) that
Are there any free tools that implement evidence-based scheduling like Joel talks about ?
Is there a free or open source library to read Excel files (.xls) directly
I have a free standing set of files not affiliated with any C# project
I use the free webhost 000webhost. The service is okay but it inserts some
i am looking for a free Webhost for JSF sites.. unfortunately i didn't find
What free profilers are there fore Windows 7 that are compatible with vs2010?
Anyone know of a FREE and easy to use PHP MySQL Datagrid class? I
I look for a free launcher or bootstrapper that checks whether the .net framework

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.