title: Remove junk trailing xml from an inputstream
body: <p>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. </p>
  
  <p>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.)</p>
  
  <p>Any suggestions on a reasonably efficient strategy? </p>
  
  <p>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.</p>
  
  <p>FWIW, this is part of an Android project, so I'm using the <code>android.util.Xml</code> class (see <a href="http://www.java2s.com/Open-Source/Android/android-core/platform-frameworks-base/android/util/Xml.java.htm" rel="nofollow">source code</a>). When I traced the exception, it took me to a <em>native</em> appendChars function that is itself being called from a network of private methods anyway, so subclassing anything seems to be unreasonably useless.</p>
  
  <p>Here's the salient bit from my stacktrace:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I guess in the end I'm asking for opinions on whether the <em>InputStream -> manually parse to OutputStream -> recreate InputStream -> pass along</em> solution is as horrible as I think it is.</p>