Content is not allowed in prolog when parsing perfectly valid XML on GAE
The encoding in your XML and XSD (or DTD) are different.
XML file header: <?xml version='1.0' encoding='utf-8'?>
XSD file header: <?xml version='1.0' encoding='utf-16'?>
Another possible scenario that causes this is when anything comes before the XML document type declaration. i.e you might have something like this in the buffer:
helloworld<?xml version="1.0" encoding="utf-8"?>
or even a space or special character.
There are some special characters called byte order markers that could be in the buffer.
Before passing the buffer to the Parser do this...
String xml = "<?xml ...";
xml = xml.trim().replaceFirst("^([\\W]+)<","<");
Content is not allowed in prolog error yet nothing before XML declaration
Elaborating on what @MartinHonnen has already helpfully commented...
The error,
Content is not allowed in prolog.
arises because the XML prolog, which is everything before the root element in an XML document, has textual content that is not allowed. The error does not necessarily have to have occurred before the XML declaration.
Specifically, the prolog
in XML is defined in the context of an XML document
:
[1] document ::= prolog element Misc*
Note that prolog
precedes element
, the single root element of the XML document.
Most answers focus on the problem where there is text (visible or invisible) at the beginning of the prolog
, before the XML declaration, but note that non-whitespace text cannot appear anywhere within or after the prolog
either:
[22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
[23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
[24] VersionInfo ::= S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"')
[25] Eq ::= S? '=' S?
[26] VersionNum ::= '1.' [0-9]+
[27] Misc ::= Comment | PI | S
In your case, you have Test material...
text content appearing between the XML declaration (XMLDecl
) and the root element (element
). A comment, processing instruction, or whitespace can appear there, but not text.
Fatal Error :1:1: Content is not allowed in prolog
I'm turning my comment to an answer, so it can be accepted and this question no longer remains unanswered.
The most likely cause of this is a malformed response, which includes characters before the initial <?xml …>
. So please have a look at the document as transferred over HTTP, and fix this on the server side.
Fatal Error - Content is not allowed in prolog
The document and sample code you provided works fine in Java 1.8u25:
import static org.junit.Assert.*;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class FatalErrorTest
{
@Test
public void as_given() throws SAXException, IOException, ParserConfigurationException
{
String xml ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<?xfa generator=\"ff99v250_01\" APIVersion=\"1.4.3139.0\"?>\r\n<jfxpf:XPF xmlns:jfxpf=\"http://www.xfa.com/schema/xml-package\">\r\n <jfxpf:Package>\r\n <jfxpf:Resource Location=\"GenReq\">\r\n <jfxpf:Link ContentType=\"application/x-jetform-cft\" />\r\n </jfxpf:Resource>\r\n <jfxpf:Resource Location=\"default.xml\">\r\n <jfxpf:Content ContentType=\"text/xml\" Location=\"default.xml\">\r\n <xfa:Data xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\">\r\n <xfa:DataGroup>\r\n <data xmlns:xfe=\"http://www.xfa.org/schema/xfa-events/1.0\" xfe:script=\"$config.proto.common.template.uri='GenReq'\" xfe:event=\"$config:load\">\r\n <?jetform ^Dat ^page 1?>\r\n <FR_NAME>Administrator</FR_NAME>\r\n <JFWF_DELEGATE />\r\n <ADHOC_DLN_ACTOR />\r\n <ADHOC_DLN_MSG />\r\n <ADHOC_DLN_TIME />\r\n <ADHOC_DLN_UNITS>Days</ADHOC_DLN_UNITS>\r\n <ADHOC_RMD_MSG />\r\n <ADHOC_RMD_TIME />\r\n <ADHOC_RMD_UNITS>Days</ADHOC_RMD_UNITS>\r\n <ADHOC_RPT_TIME />\r\n <ADHOC_RPT_UNITS>Days</ADHOC_RPT_UNITS>\r\n <CIRCULATETO />\r\n <COMPLETION />\r\n <FOLLOWUP />\r\n <MSGSUBJECT />\r\n <OTHERFIELD />\r\n <PRIORITY>Low</PRIORITY>\r\n <REQUEST />\r\n <RESPONSE />\r\n <Submit />\r\n <ADHOC_VALIDDATA>True</ADHOC_VALIDDATA>\r\n <JFWF_TRANID>2xxyg9sffane7pwd5j8yv9t49s.1</JFWF_TRANID>\r\n <JFWF_INSTRUCTION>Initiate a General Request. Fill the request form, then identify the next participant.</JFWF_INSTRUCTION>\r\n <JFWF_TRANSPORT>HTTP</JFWF_TRANSPORT>\r\n <JFWF_STATUS>RECEIVED</JFWF_STATUS>\r\n <JFWF_ACTION />\r\n <JFWF_CHOICE>*Select Next Participant,Cancel</JFWF_CHOICE>\r\n <JFWF_VERSION>6.2</JFWF_VERSION>\r\n <JFWF_READONLY>1</JFWF_READONLY>\r\n </data>\r\n </xfa:DataGroup>\r\n </xfa:Data>\r\n </jfxpf:Content>\r\n </jfxpf:Resource>\r\n </jfxpf:Package>\r\n</jfxpf:XPF>";
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new InputSource(new StringReader(xml)));
assertNotNull(doc);
}
}
org.xml.sax.SAXParseException: Content is not allowed in prolog
This is often caused by a white space before the XML declaration, but it could be any text, like a dash or any character. I say often caused by white space because people assume white space is always ignorable, but that's not the case here.
Another thing that often happens is a UTF-8 BOM (byte order mark), which is allowed before the XML declaration can be treated as whitespace if the document is handed as a stream of characters to an XML parser rather than as a stream of bytes.
The same can happen if schema files (.xsd) are used to validate the xml file and one of the schema files has an UTF-8 BOM.
Fatal Error :1:1: Content is not allowed in prolog. org.xml.sax.SAXParseException
StringReader("myfile.xml")
takes a string argument that must be XML, not a filename. The parser is reading the string literal, myfile.xml
, (not the file contents of myfile.xml
) and failing immediately because an XML document may not begin with an m
character.
Change
Document doc = dBuilder.parse(new InputSource(new StringReader("myfile.xml")));
to
Document doc = dBuilder.parse(new InputSource("myfile.xml"));
Related Topics
How to Check the Type of a Value from a JSONobject
Java String.Split() Sometimes Giving Blank Strings
Java String Array: Is There a Size of Method
How to Ignore Certain Elements When Comparing Xml
How to Check a Timeperiod Is Overlapping Another Time Period in Java
Scanner Class Skips Over Whitespace
How to Set Eclipse Console Locale/Language
Java - Scroll to Specific Text Inside Jtextarea
Why Do Java If Statement Fail When It Ends in Semicolon
Pdfbox Encode Symbol Currency Euro
Drag and Drop Custom Object from Jlist into Jlabel
Production Settings File for Log4J
Get the Week Start and End Date Given a Current Date and Week Start
Java.Io.Streamcorruptedexception: Invalid Type Code: 00
Parsing Dates of the Format "January 10Th, 2010" in Java? (With Ordinal Indicators, St|Nd|Rd|Th)
JPA SQL Server No Dialect Mapping for Jdbc Type: -9
What Is the Purpose of Accesstype.Field, Accesstype.Property and @Access