469,352 Members | 1,694 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

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.SAXParseException: 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:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema
-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><postSubmission xmlns="http://service.arm.hud.gov/"><submissionHeader><agcHcsId>1</agcHcsId><agcName>test</agcName><systemName>123</systemName><cmsSubId>123456</cmsSubId><subFlag>0</subFlag></submissionHeader><agcType>test</agcType><submissionData>PD94bWwgdmVyc2lvbj0iMS4wIi BlbmNvZGluZz0idXRmLTgiPz48dG5zOlN1Ym1pc3Npb25EYXRh IHhzaTpzY2hlbWFMb2NhdGlvbj0iaHR0cDovL2dvdi5odWQuYX JtL2FybV9kYXRhYmFnXzNfMS54c2QiIHhtbG5zO
nhzaT0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxT......e tc............................</submissionData></postSubmission></soap:Body></soap:Envelope>
Mar 17 '09 #1
7 7932
jkmyoung
2,057 Expert 2GB
This is a common problem:
http://forums.sun.com/thread.jspa?threadID=567285

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
Mar 17 '09 #2
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 .
Mar 17 '09 #3
jkmyoung
2,057 Expert 2GB
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.
http://msdn.microsoft.com/en-us/libr...eamwriter.aspx
To create a StreamWriter using UTF-8 encoding and a BOM, consider using a constructor that specifies encoding, such as StreamWriter(String, Boolean, Encoding).
Mar 18 '09 #4
sure, here is an abbreviated look at the code (i took out the impertinent parts of the doPostSubmission method)...

Expand|Select|Wrap|Line Numbers
  1. private static void doPostSubmission(ArmServiceImplServiceWse armService)
  2. {
  3. ASCIIEncoding encoding = new ASCIIEncoding();
  4. MemoryStream ms = new MemoryStream();
  5. StringBuilder sb = new StringBuilder();
  6. TextReader tr = null;
  7. XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding());
  8.  
  9. //write xml document header
  10. xmlWriter.WriteStartDocument();
  11.  
  12. //rest of xml is created here...
  13.  
  14. //finalize xml document
  15. xmlWriter.WriteEndDocument();
  16.  
  17. xmlWriter.Flush();
  18.  
  19. //send memorystream to xml file to check with hex editor for incorrect prolog
  20. ms.Position = 0;
  21.  
  22. using (FileStream fs = File.Open(@"c:\test.xml", FileMode.Create, FileAccess.Write))
  23. {
    ms.WriteTo(fs);
  24.  
  25. fs.Flush();
  26. fs.Close();}
  27.  
  28. //here i create a string and a byte array and send both to the web service
  29. //for testing - both get the content not allowed in prolog exception...
  30.  
  31. //***************************************************************************                
  32. //derive string from memory stream using a text reader
  33. //i am trying to use a string because that is what the 3rd party's example used...
  34. ms.Position = 0;
  35.  
  36. tr = new StreamReader(ms);
  37. ms.Seek(0, SeekOrigin.Begin);
  38. str = tr.ReadToEnd();
  39. //***************************************************************************
  40.  
  41. //get byte array directly from memory stream to pass straight to postSubmission.submissionData
  42. ms.Position = 0;
  43.  
  44. byte[] byteArray = ms.ToArray();
  45. //***************************************************************************
  46.  
  47. xmlWriter.Close();
  48.  
  49. try
  50. {
  51.        postSubmission.submissionData = byteArray;
  52.        armService.postSubmission(postSubmission);
  53. }
  54. catch (Exception ex)
  55. {
  56.        Console.WriteLine("\r\n1 ***ERROR ENCOUNTERED with passing byte array directly***\r\n" + ex.Message);
  57. }
  58. try 
  59. {
  60.        postSubmission.submissionData = encoding.GetBytes(str);
  61.        armService.postSubmission(postSubmission);
  62. }
  63. catch (Exception ex)
  64. {
  65.        Console.WriteLine("\r\n2 ***ERROR ENCOUNTERED with string from text reader***\r\n" + ex.Message);
  66. }
  67. }
  68.  
  69.  
(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:

Expand|Select|Wrap|Line Numbers
  1. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")]
  2.     [System.SerializableAttribute()]
  3.     [System.Diagnostics.DebuggerStepThroughAttribute()]
  4.     [System.ComponentModel.DesignerCategoryAttribute("code")]
  5.     [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://service.arm.hud.gov/")]
  6.     public partial class postSubmission {
  7.  
  8.         private submissionHeader submissionHeaderField;
  9.  
  10.         private string agcTypeField;
  11.  
  12.         private byte[] submissionDataField;
  13.  
  14.         /// <remarks/>
  15.         public submissionHeader submissionHeader {
  16.             get {
  17.                 return this.submissionHeaderField;
  18.             }
  19.             set {
  20.                 this.submissionHeaderField = value;
  21.             }
  22.         }
  23.  
  24.         /// <remarks/>
  25.         public string agcType {
  26.             get {
  27.                 return this.agcTypeField;
  28.             }
  29.             set {
  30.                 this.agcTypeField = value;
  31.             }
  32.         }
  33.  
  34.         /// <remarks/>
  35.         [System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")]
  36.         public byte[] submissionData {
  37.             get {
  38.                 return this.submissionDataField;
  39.             }
  40.             set {
  41.                 this.submissionDataField = value;
  42.             }
  43.         }
  44.     }
  45.  
here is the definition of the postSubmission method:

Expand|Select|Wrap|Line Numbers
  1. [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", OneWay=true, Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
  2. public void postSubmission([System.Xml.Serialization.XmlElementAttribute("postSubmission", Namespace="http://service.arm.hud.gov/")] postSubmission postSubmission1) 
  3. {
  4.             this.Invoke("postSubmission", new object[] { postSubmission1 });
  5. }

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
File Type: txt test.xml.txt (19.6 KB, 614 views)
Mar 19 '09 #5
jkmyoung
2,057 Expert 2GB
Added redirect to .net thread in case anyone there is also familar with this problem.
Mar 19 '09 #6
thanks...i hope that this issue isn't so far out that no one will be able to help...
Mar 20 '09 #7
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.
Feb 11 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Greg Scharlemann | last post: by
reply views Thread by Dominique | last post: by
4 posts views Thread by Nicolas Fleury | last post: by
9 posts views Thread by Stanimir Stamenkov | last post: by
10 posts views Thread by Richard R Plourde | last post: by
5 posts views Thread by Nick Flandry | last post: by
15 posts views Thread by Mike | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.