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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:18:01+00:00 2026-06-18T00:18:01+00:00

I am trying to read the XML file being posted using http doPost method.

  • 0

I am trying to read the XML file being posted using http doPost method. While parsing using SAXParser it throws an Exception:

Content is not allowed in prolog.

The doPost code is:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
{    
    ServletInputStream httpIn = request.getInputStream();        
    byte[] httpInData = new byte[request.getContentLength()];
    StringBuffer readBuffer = new StringBuffer();
    int retVal = -1;
    while ((retVal = httpIn.read(httpInData)) != -1)
    {
        for (int i=0; i<retVal; i++)
        {
            readBuffer.append(Character.toString((char)httpInData[i]));
        }                   
    }

    System.out.println("XML Received" + readBuffer);
    try 
    {
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(
            readBuffer.toString().getBytes("UTF-8"));
        final XmlParser xmlParser = new XmlParser();
        parser.parse(inputStream, xmlParser);               
    }
    catch (Exception e)
    {
        System.out.println("Exception parsing the xml request" + e);
    }
}

This is the JUnit I am testing with:

public static void main(String args[])
{    
    StringBuffer buffer = new StringBuffer();   
    buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    buffer.append("<person>");
    buffer.append("<name>abc</name>");
    buffer.append("<age>25</age>");
    buffer.append("</person>");

    try
    {
        urlParameters = URLEncoder.encode(buffer.toString(), "UTF-8");
    } 
    catch (Exception e1)
    {       
        e1.printStackTrace();
    }

    String targetURL = "http://localhost:8888/TestService";

    URL url;
    HttpURLConnection connection = null;  
    try 
    {
        //Create connection
        url = new URL(targetURL);
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/xml");
        connection.setRequestProperty("Content-Length", "" + 
            Integer.toString(urlParameters.getBytes("UTF-8").length));
        connection.setRequestProperty("Content-Language", "en-US");  
        connection.setUseCaches (false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream (
            connection.getOutputStream ());
        wr.writeBytes (urlParameters);
        wr.flush ();
        wr.close ();
    }
    catch (Exception e)
    {
        e.printStackTrace();           
    }

The XML output in the servlet that I am getting is something like this:

XML Received %3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%3Cperson%3E%

So this is throwing an exception in SAXparser:

What am I doing wrong? Am I sending the XML in wrong way or reading it wrong way?

  • 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-18T00:18:02+00:00Added an answer on June 18, 2026 at 12:18 am

    You assume

    httpInData[i]
    

    is a char, while it is a byte. Your content being UTF-8, that makes a big difference. Use a Reader instead.

    Then, you are URLEncoding your XML, which is useless, as it is a POST data. Don’t encode it, simply send the data.

    replace

    urlParameters = URLEncoder.encode(buffer.toString(), "UTF-8");
    

    by

    urlParameters = buffer.toString();
    

    Also, the name urlParameter is poorly chosen, as this is a single post body, doesn’t go in the url, and isn’t really a parameter.

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

Sidebar

Related Questions

I am trying to read an XML file using objective C language and parse
Hi I am trying to read XML File using XLinq and binding the values
I'm trying to read an xml file using xmlTextReader and store it in a
I'm trying to read a Log4net XMLLayout output log file using Log4View. <?xml version=1.0?>
I am Trying to Read xml file of size 10 MB. I am Using
I am trying to read this XML file using PHP and I have two
I have one .net windows application I'm trying to read .xml file from c#
I am trying to read in an XML file that I have saved to
I have an XML file that I'm trying to read from here , and
I'm trying to read data from an xml file and display it in a

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.