Content Is Not Allowed in Prolog Saxparserexception

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"));

SAXParseException: Content is not allowed in prolog

Your xml file has some invisible chars (most likely the BOM) at the start (before <?xml version="1.0" encoding="UTF-8"?>) which is not allowed in xml. you could view it using a hex editor. Simplest way to fix it is to create an empty text file and copy the content into it, change the extension to xml.

Check this answer for further help.

From http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html

UTF8 file are a special case because it is not recommended to add a BOM to them because it can break other tools like Java. In fact, Java assumes the UTF8 don't have a BOM so if the BOM is present it won't be discarded and it will be seen as data.

JSP: SAXParseException - Content is not allowed in prolog

Looks like Java StringReader accepts string, not URI to a file which content will be read as string. So in this case, your code passes value "test\foo.xml" to builder.parse() instead of passing string content of 'foo.xml' file, hence the error.

That said, use builder.parse(file)* instead :

String xmlFileURI = "test\\foo.xml";
File file = new File (xmlFileURI);
if (file.exists ())
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
}

*) Not a Java programmer, got the idea from here. And don't forget to restart Tomcat, as OP mentioned, for this changes to take effect.

org.xml.sax.SAXParseException; Content is not allowed in prolog

The reason I found later was, that I used StringInputStream class and didn't realize, that it is org.hsqldb.lib.StringInputStream.StringInputStream(String).

When I replaced this one with

  • java.io.StringBufferInputStream.StringBufferInputStream(String) // deprecated
  • or java.io.StringReader.StringReader(String) // sniff accepts reader too
  • or java.io.ByteArrayInputStream.ByteArrayInputStream(byte[])

...it worked. I didn't investigate why the previous one from hsqldb package is bad in this case...

Content is not allowed in Prolog SAXParserException

This error is probably related to a byte order mark (BOM) prior to the actual XML content. You need to parse the returned String and discard the BOM, so SAXParser can process the document correctly.

You will find a possible solution here.

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.

org.xml.sax.SAXParseException : Content is not allowed in prolog

Problem is with your mybatis config:

mybatis:
configuration:
map-underscore-to-camel-case: true
type-aliases-package: com.ryanqy.entity
mapper-locations: mapper

change mapper-locations to:

  mapper-locations: mapper/UserMapper.xml
#OR
mapper-locations: mapper/*.xml

If you change, you receive error with missing DOCTYPE in your mapper XML, so add something like this

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">


Related Topics



Leave a reply



Submit