content not allowed in prolog exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nluciano
    New Member
    • Mar 2009
    • 4

    content not allowed in prolog exception

    I am trying to send xml to a java-based web service given to me by a thrid party via a c#.NET application, and I get the "org.xml.sax.SA XParseException : Content is not allowed in prolog" error.

    I have verified the xml against the schema, and I passed the memorystream I am using to hold the xml to an .xml file, then opened the file with a hex editor to make sure that there were no undesired characters in the prolog, and there are none. When opened, the first characters in the file are

    <?xml version="1.0" encoding="utf-8"?>

    The class I was given to use to send the xml data to the web service accepts a byte array. I figure that creating the xml using an XmlTextWriter to a utf-8 encoded memorystream, then sending the contents of the stream to a byte array is the most direct method.

    I have done a lot of research and tried all the possbilities around this issue that I could find, but nothing works. Could someone please help? Thanks in advance.

    By the way, here is a portion of what the web service returns to me. In the payload of the SOAP message, should the data after the <submissionData > element look like that, or be readable xml like the content before it?


    Messages:
    Message:

    Payload: <?xml version="1.0" encoding="utf-8"?><soap:Envel ope xmlns:soap="htt p://schemas.xmlsoap .org/soap/envelope/" xmlns:xsi="http ://www.w3.org/2001/XMLSchema
    -instance" xmlns:xsd="http ://www.w3.org/2001/XMLSchema"><soa p:Body><postSub mission xmlns="http://service.arm.hud .gov/"><submissionHe ader><agcHcsId> 1</agcHcsId><agcNa me>test</agcName><system Name>123</systemName><cms SubId>123456</cmsSubId><subFl ag>0</subFlag></submissionHeade r><agcType>test </agcType><submis sionData>PD94bW wgdmVyc2lvbj0iM S4wIiBlbmNvZGlu Zz0idXRmLTgiPz4 8dG5zOlN1Ym1pc3 Npb25EYXRhIHhza TpzY2hlbWFMb2Nh dGlvbj0iaHR0cDo vL2dvdi5odWQuYX JtL2FybV9kYXRhY mFnXzNfMS54c2Qi IHhtbG5zO
    nhzaT0iaHR0cDov L3d3dy53My5vcmc vMjAwMS9YTUxT.. ....etc........ ............... .....</submissionData> </postSubmission> </soap:Body></soap:Envelope>
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    This is a common problem:
    Oracle Forums is a community platform where you can discuss Oracle products and services, collaborate with peers, and connect with Oracle experts.


    Key post:
    I've also found that some text editors will add a BOM (Byte-Order Mark) to UTF-8 files. Java expects the BOM on files with other encoding, but can get confused when it finds the UTF-8 BOM, and reports unknown content in the prolog.
    You might have to specify UTF-8 encoding when you are reading the file, or ask for the file in a different format, eg UTF-16 or ISO-8859-1

    Comment

    • nluciano
      New Member
      • Mar 2009
      • 4

      #3
      thanks for the response.

      i am not reading from a file. i am using the c# xmlTextWriter to write xml dynamically into a utf-8 encoded memorystream, then parsing the stream into a byte array which is required by the class that sends the xml to the java web service. the web service responds with the "content not allowed in prolog" exception. i sent the streamwriter to an .xml file and checked in a hex editor, plus analyzed the byte array in the debugger and there was no bom or other garbage characters in the beginning .

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        What is the input stream class you are using? Could you post the specific code you're using to receive and transfer the input?

        The act of using the StreamWriter may remove whatever BOM comes in the data.
        Implements a TextWriter for writing characters to a stream in a particular encoding.

        To create a StreamWriter using UTF-8 encoding and a BOM, consider using a constructor that specifies encoding, such as StreamWriter(St ring, Boolean, Encoding).

        Comment

        • nluciano
          New Member
          • Mar 2009
          • 4

          #5
          sure, here is an abbreviated look at the code (i took out the impertinent parts of the doPostSubmissio n method)...

          Code:
          private static void doPostSubmission(ArmServiceImplServiceWse armService)
          {[INDENT]
          ASCIIEncoding encoding = new ASCIIEncoding();
          MemoryStream ms = new MemoryStream();
          StringBuilder sb = new StringBuilder();
          TextReader tr = null;
          XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding());
          
          //write xml document header
          xmlWriter.WriteStartDocument();
          
          //rest of xml is created here...
          
          //finalize xml document
          xmlWriter.WriteEndDocument();
          
          xmlWriter.Flush();
          
          //send memorystream to xml file to check with hex editor for incorrect prolog
          ms.Position = 0;
          
          using (FileStream fs = File.Open(@"c:\test.xml", FileMode.Create, FileAccess.Write))
          {[INDENT]ms.WriteTo(fs);
          
          fs.Flush();
          fs.Close();[/INDENT]
          }
          
          //here i create a string and a byte array and send both to the web service
          //for testing - both get the content not allowed in prolog exception...
          
          //***************************************************************************                
          //derive string from memory stream using a text reader
          //i am trying to use a string because that is what the 3rd party's example used...
          ms.Position = 0;
          
          tr = new StreamReader(ms);
          ms.Seek(0, SeekOrigin.Begin);
          str = tr.ReadToEnd();
          //***************************************************************************
          
          //get byte array directly from memory stream to pass straight to postSubmission.submissionData
          ms.Position = 0;
                          
          byte[] byteArray = ms.ToArray();
          //***************************************************************************
          
          xmlWriter.Close();
          
          try
          {
                 postSubmission.submissionData = byteArray;
                 armService.postSubmission(postSubmission);
          }
          catch (Exception ex)
          {
                 Console.WriteLine("\r\n1 ***ERROR ENCOUNTERED with passing byte array directly***\r\n" + ex.Message);
          }
          try 
          {
                 postSubmission.submissionData = encoding.GetBytes(str);
                 armService.postSubmission(postSubmission);
          }
          catch (Exception ex)
          {
                 Console.WriteLine("\r\n2 ***ERROR ENCOUNTERED with string from text reader***\r\n" + ex.Message);
          }
          [/INDENT]
          }
          (the following was provided by the third party as part of an sdk that developers were told to use to develop a client to access the web service)

          here is the submissionData class:

          Code:
          [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")]
              [System.SerializableAttribute()]
              [System.Diagnostics.DebuggerStepThroughAttribute()]
              [System.ComponentModel.DesignerCategoryAttribute("code")]
              [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://service.arm.hud.gov/")]
              public partial class postSubmission {
                  
                  private submissionHeader submissionHeaderField;
                  
                  private string agcTypeField;
                  
                  private byte[] submissionDataField;
                  
                  /// <remarks/>
                  public submissionHeader submissionHeader {
                      get {
                          return this.submissionHeaderField;
                      }
                      set {
                          this.submissionHeaderField = value;
                      }
                  }
                  
                  /// <remarks/>
                  public string agcType {
                      get {
                          return this.agcTypeField;
                      }
                      set {
                          this.agcTypeField = value;
                      }
                  }
                  
                  /// <remarks/>
                  [System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")]
                  public byte[] submissionData {
                      get {
                          return this.submissionDataField;
                      }
                      set {
                          this.submissionDataField = value;
                      }
                  }
              }
          here is the definition of the postSubmission method:

          Code:
          [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", OneWay=true, Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
          public void postSubmission([System.Xml.Serialization.XmlElementAttribute("postSubmission", Namespace="http://service.arm.hud.gov/")] postSubmission postSubmission1) 
          {
                      this.Invoke("postSubmission", new object[] { postSubmission1 });
          }

          i have also attached the xml output (i had to save it with a .txt extension as the forum would not let me upload a file with an .xml extension).

          thanks, and let me know if you need to see anything else.
          Attached Files

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            Added redirect to .net thread in case anyone there is also familar with this problem.

            Comment

            • nluciano
              New Member
              • Mar 2009
              • 4

              #7
              thanks...i hope that this issue isn't so far out that no one will be able to help...

              Comment

              • JoseLuis
                New Member
                • Feb 2012
                • 1

                #8
                Has anyone found a solution to this problem?
                I have to create a .NET web service to an existing java client which spits out this error when it tries to send the file. I have no access to the client.

                Comment

                Working...