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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:58:47+00:00 2026-05-25T23:58:47+00:00

Well I am trying to connect to one remote server via socket, and I

  • 0

Well I am trying to connect to one remote server via socket, and I get big xml responses back from socket, delimited by a ‘\n’ character.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <data>
       .......
       .......
    </data>
</Response>\n   <---- \n acts as delimiter 
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <data>
        ....
        ....
    </data>
</Response>\n
..

I am trying to parse these xml using SAX Parser. Ideally I want to get one full response to a string by searching for ‘\n’ and give this response to parser. But since my single response is very large, I am getting outOfMemory Exception when holding such a large xml in string..So the only option remained was to stream the xml to SAX.

SAXParserFactory spfactory = SAXParserFactory.newInstance();
SAXParser saxParser = spfactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();

xmlReader.setContentHandler(new MyDefaultHandler(context));

InputSource xmlInputSource  =   new InputSource(new    
                    CloseShieldInputStream(mySocket.getInputStream()));
xmlReader.parse(xmlInputSource);

I am using closeShieldInputStream to prevent SAX closing my socket stream on exception because of ‘\n’. I asked a previous question on that..

Now sometimes I am getting Parse Error

org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 8: not well-formed (invalid token)

I searched for it and found out this error normally comes when the encoding of actual xml is not same as what SAX expecting. I wrote a C program and print out the xml, and all my xml is encoded by UTF-8.

Now my question..

  1. Is there any other reason for the above given error in xml parsing
    other than encoding issue
  2. Is there any way to print (or write to any file) the input to SAX as
    it streams from socket?

After trying Hemal Pandya’s answer..

OutputStream log = new BufferedOutputStream(new FileOutputStream("log.txt"));
InputSource xmlInputSource  =   new InputSource(new CloseShieldInputStream(new   
                                    TeeInputStream(mReadStream, log)));
xmlReader.parse(xmlInputSource);

a new file with name log.txt getting created when I mount the SDCard but it is empty..Am I using this right?

Well Finally how I done it..

I worked it out with TeeInputStream itself..thanks Hemal Pandya for suggesting that..

//open a log file in append mode..
OutputStream log = new BufferedOutputStream(new FileOutputStream("log.txt",true));
InputSource xmlInputSource  =   new InputSource(new CloseShieldInputStream(new   
                                        TeeInputStream(mReadStream, log)));
try{
  xmlReader.parse(xmlInputSource);
  //flush content in the log stream to file..this code only executes if parsing completed successfully 
  log.flush();
}catch(SaxException e){
  //we want to get the log even if parsing failed..So we are making sure we get the log in either case..
  log.flush();
}
  • 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-25T23:58:47+00:00Added an answer on May 25, 2026 at 11:58 pm

    Is there any way to print (or write to any file) the input to SAX as
    it streams from socket?

    Apache Commons has a TeeInputStream that should be useful.

    OutputStream log = new BufferedOutputStream(new FileOutputtStream("response.xml"));
    InputSource xmlInputSource  =   new InputSource(new    
                        CloseShieldInputStream(new TeeInputStream(mySocket.getInputStream(), log)));
    

    I haven’t used it, you might want to try it first in a standalone program to figure out close semantics, though looking at docs and your requirements it looks like you would want to close it separately at end.

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

Sidebar

Related Questions

Well, I was trying to select rows from one table if there are no
well iam trying to get version of IE used by user by using $.browser.version
Well, here's a nice obscure one. I'm trying to compile the latest open transport
Well guys, I'm trying to get that MongoDB feature working for hours: http://www.mongodb.org/display/DOCS/Http+Interface I
I'm trying to connect to REST web server and I don't know what is
I am trying to connect securely to RDS instance from Hibernate. Relevant properties in
One of my applications connect to a server module that is normally installed on
I'm trying to use Oauth to connect to salesforce from a .NET app. I'm
I have client and server programs which now communicate via TCP. I'm trying out
I'm trying to connect to a named SQL instance on the server. There is

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.