Content is not allowed in prolog ошибка xml

I’ve been beating my head against this absolutely infuriating bug for the last 48 hours, so I thought I’d finally throw in the towel and try asking here before I throw my laptop out the window.

I’m trying to parse the response XML from a call I made to AWS SimpleDB. The response is coming back on the wire just fine; for example, it may look like:

<?xml version="1.0" encoding="utf-8"?> 
<ListDomainsResponse xmlns="http://sdb.amazonaws.com/doc/2009-04-15/">
    <ListDomainsResult>
        <DomainName>Audio</DomainName>
        <DomainName>Course</DomainName>
        <DomainName>DocumentContents</DomainName>
        <DomainName>LectureSet</DomainName>
        <DomainName>MetaData</DomainName>
        <DomainName>Professors</DomainName>
        <DomainName>Tag</DomainName>
    </ListDomainsResult>
    <ResponseMetadata>
        <RequestId>42330b4a-e134-6aec-e62a-5869ac2b4575</RequestId>
        <BoxUsage>0.0000071759</BoxUsage>
    </ResponseMetadata>
</ListDomainsResponse>

I pass in this XML to a parser with

XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(response.getContent());

and call eventReader.nextEvent(); a bunch of times to get the data I want.

Here’s the bizarre part — it works great inside the local server. The response comes in, I parse it, everyone’s happy. The problem is that when I deploy the code to Google App Engine, the outgoing request still works, and the response XML seems 100% identical and correct to me, but the response fails to parse with the following exception:

com.amazonaws.http.HttpClient handleResponse: Unable to unmarshall response (ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.): <?xml version="1.0" encoding="utf-8"?> 
<ListDomainsResponse xmlns="http://sdb.amazonaws.com/doc/2009-04-15/"><ListDomainsResult><DomainName>Audio</DomainName><DomainName>Course</DomainName><DomainName>DocumentContents</DomainName><DomainName>LectureSet</DomainName><DomainName>MetaData</DomainName><DomainName>Professors</DomainName><DomainName>Tag</DomainName></ListDomainsResult><ResponseMetadata><RequestId>42330b4a-e134-6aec-e62a-5869ac2b4575</RequestId><BoxUsage>0.0000071759</BoxUsage></ResponseMetadata></ListDomainsResponse>
javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
    at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(Unknown Source)
    at com.amazonaws.transform.StaxUnmarshallerContext.nextEvent(StaxUnmarshallerContext.java:153)
    ... (rest of lines omitted)

I have double, triple, quadruple checked this XML for ‘invisible characters’ or non-UTF8 encoded characters, etc. I looked at it byte-by-byte in an array for byte-order-marks or something of that nature. Nothing; it passes every validation test I could throw at it. Even stranger, it happens if I use a Saxon-based parser as well — but ONLY on GAE, it always works fine in my local environment.

It makes it very hard to trace the code for problems when I can only run the debugger on an environment that works perfectly (I haven’t found any good way to remotely debug on GAE). Nevertheless, using the primitive means I have, I’ve tried a million approaches including:

  • XML with and without the prolog
  • With and without newlines
  • With and without the «encoding=» attribute in the prolog
  • Both newline styles
  • With and without the chunking information present in the HTTP stream

And I’ve tried most of these in multiple combinations where it made sense they would interact — nothing! I’m at my wit’s end. Has anyone seen an issue like this before that can hopefully shed some light on it?

Thanks!

If you are working with XML files, you may have encountered an error message that says «Content is not allowed in prolog». This error can be frustrating, but it is usually easy to fix. In this guide, we will walk you through the steps to resolve this error.

What causes the ‘Content Is Not Allowed in Prolog’ Error?

The ‘Content is not allowed in prolog’ error message appears when there is invalid content before the XML declaration in the file. The XML declaration is the first line of an XML file and typically contains the version and encoding information.

There are several reasons why invalid content may appear before the XML declaration, such as:

  • A hidden character or whitespace before the XML declaration
  • Malformed HTML tags or comments
  • Invalid encoding
  • Incorrect file type

How to Fix the ‘Content Is Not Allowed in Prolog’ Error

Follow the steps below to fix the ‘Content is not allowed in prolog’ error for XML files:

  1. Open the XML file in a text editor, such as Notepad or Sublime Text.
  2. Look for any invalid content before the XML declaration, such as hidden characters or whitespace. Delete any invalid content and save the file.
  3. Check for malformed HTML tags or comments. Fix any errors and save the file.
  4. Check the encoding of the file. The encoding should match the encoding declared in the XML declaration. If the encoding is incorrect, change it and save the file.
  5. Ensure that the file is saved as an XML file. If the file has a different extension, such as .txt, rename it to have the .xml extension.

FAQ

1. How do I open an XML file in a text editor?

To open an XML file in a text editor, right-click on the file and select ‘Open with’ and choose a text editor program, such as Notepad or Sublime Text.

2. What are hidden characters?

Hidden characters are characters that are not visible in the text editor but can affect the formatting of the document. Examples of hidden characters include spaces, tabs, and line breaks.

3. How do I check the encoding of an XML file?

You can check the encoding of an XML file by looking at the XML declaration at the beginning of the file. The encoding is specified in the ‘encoding’ attribute.

4. Can I use an XML file with a .txt extension?

Technically, you can use an XML file with any extension. However, it is best practice to use the .xml extension to avoid confusion and ensure that the file is recognized as an XML file.

5. What should I do if the error message persists after trying the steps above?

If the error message persists after trying the steps above, it may be caused by a more complex issue. In this case, it may be helpful to seek assistance from a developer or technical support team.

Conclusion

The ‘Content is not allowed in prolog’ error message can be frustrating, but it is usually easy to fix by following the steps above. By checking for hidden characters, malformed HTML tags, and correct encoding, you can ensure that your XML files are error-free and ready for use.

If you have any additional questions or concerns, feel free to consult the FAQ section or seek assistance from a technical expert.

  • Common XML Errors and How to Fix Them
  • How to Fix “Content is not allowed in prolog” Error in XML Files

Table of Contents

  • Sax Error Due to Invalid Text Before XML Declaration
  • Byte Order Mark (BOM) At the Beginning of the XML File
  • Passing a Non Existent File to Parser
  • Different Encoding Formats Causing the Parser Error
  • Conclusion

This article discusses the SAX Error – Content is not allowed in prolog.

The SAX parser is the XML parsing API that you can use to process the XML files. However, while using the SAX parser, you may encounter SAX error – content is not allowed in prolog.

Sax Error Due to Invalid Text Before XML Declaration

The XML files are structured using tags. Therefore, each XML file follows specified syntax.

If you place an unknown or invalid character before the XML declaration, you will get the aforementioned error while trying to parse the file using SAX error.

Let us see an example using the following XML file.

!<?xml version=«1.0» encoding=«utf-8»?>

<person>

    <name> Mohtashim Nawaz </name>

    <age> 24 </age>

    <prof> Software Engineer </prof>

</person>

The code to parse the file is given below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

package java2blog;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class XmlParser

{

    public static void main(String[] args)

    {  

        SAXParserFactory f = SAXParserFactory.newInstance();

        try

        {

            SAXParser parser = f.newSAXParser();

            parser.parse(«sample.xml», new DefaultHandler());

        }

        catch (ParserConfigurationException | SAXException | IOException e)

        {

            e.printStackTrace();

        }

    }

}

Output:

org.xml.sax.SAXParseException; systemId: file:///home/stark/eclipse-workspace-java/java2blog/sample.xml; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.

The parser will raise the error. However, you can correct it by removing the extra characters and changing the file as given below.

<?xml version=«1.0» encoding=«utf-8»?>

<person>

    <name> Mohtashim Nawaz </name>

    <age> 24 </age>

    <prof> Software Engineer </prof>

</person>

Observe that this XML file does not have (!) symbol at the beginning.

Byte Order Mark (BOM) At the Beginning of the XML File

The Byte Order Mark is a special unicode character that can indicate different things. The text editors may insert the BOM character at the beginning of the file automatically.

While parsing the XML file with the BOM character inserted in the beginning, you may encounter the SAX parser error if the file is parsed as stream of characters instead of stream of bytes.

However, it might not always be the case as in the latest version of Java the SAX parser can parse the BOM character correctly.

You can add or remove the Byte Order Mark character from the file using the code as well as manually in the text editor. Most of the text editors provide options to add or remove the BOM character.

Passing a Non Existent File to Parser

If you pass a file to parser that does not exist, you shall get the SAX parser error. The same can happen if you accidentally fail to provide the correct path.

So even if the file existed, if its path is not correct, you will eventually get the parser error.

Let us see an example.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

package java2blog;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class XmlParser

{

    public static void main(String[] args)

    {  

        SAXParserFactory f = SAXParserFactory.newInstance();

        try

        {

            SAXParser parser = f.newSAXParser();

            parser.parse(«sample_unknown.xml», new DefaultHandler());

        }

        catch (ParserConfigurationException | SAXException | IOException e)

        {

            e.printStackTrace();

        }

    }

}

The “sample_unknown.xml” file does not exist.
Output:

java.io.FileNotFoundException: /home/stark/eclipse-workspace-java/java2blog/sample_unknown.xml (No such file or directory)

However note that in this case the only error is the FileNotFoundException rather than parser error.

Different Encoding Formats Causing the Parser Error

The difference between the file encoding format and the encoding format you pass to the parser can cause the parser error.

For instance, if your file is encoded into UTF-8 encoding and you somehow pass the UTF-16 encoding to the parser, you will end up getting the parser error. Therefore, you should always check for the file encoding before parsing it.

Conclusion

This is all about the SAX error – content is not allowed in prolog. You can read more about SAX here.

Hope you have enjoyed reading the article. Stay tuned for more such articles. Happy Learning!

Мы используем синтаксический анализатор SAX для анализа XML-файла и получаем следующее сообщение об ошибке:

org.xml.sax.SAXParseException; systemId: ../src/main/resources/staff.xml;

  lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.

Короче говоря, недопустимый текст или спецификация перед объявлением XML или другой кодировкой вызовут ошибку SAX – Содержимое не разрешено в прологе .

  • 1. Недопустимый текст перед объявлением XML.
  • 2. Спецификация в начале XML-файла.
  • 3. Другой формат кодирования
  • 4. Скачать Исходный Код
  • 5. Рекомендации

1. Недопустимый текст перед объявлением XML.

В начале XML-объявления любой текст вызовет Содержимое не разрешено в прологе ошибка.

Например, приведенный ниже XML-файл содержит дополнительную маленькую точку . перед объявлением XML.

.

    
        yong
        mook kim
        mkyong
        100000
    

Чтобы исправить это Удалите любой текст перед объявлением XML.


    
        yong
        mook kim
        mkyong
        100000
    

2. Спецификация в начале XML-файла.

Многие текстовые редакторы автоматически добавляют спецификацию в файл UTF-8.

Примечание Прочитайте следующие статьи:

  • Java добавляет и удаляет спецификацию из файла UTF-8
  • Википедия – Метка порядка байтов (спецификация)

Протестированный с Java 11 и Java 8, встроенный синтаксический анализатор SAX может правильно анализировать файл спецификации UTF-8; однако некоторые разработчики утверждали, что спецификация вызвала ошибку при анализе XML.

Чтобы исправить это , удалите спецификацию из файла UTF-8.

  1. Удалите спецификацию с помощью кода
  2. В notepad++ проверьте кодировку UTF-8 без спецификации .
  3. В Intellij IDEA прямо в файле выберите Удалить спецификацию .

P.S Многие редакторы текста или кода имеют функции для добавления или удаления метка порядка байтов (спецификация) для файла попробуйте найти нужную функцию в меню.

3. Другой формат кодирования

Различная кодировка также вызвала популярный XML Содержимое не допускается в прологе.

Например, XML-файл UTF-8.


    
        mkyong
        support
        5000
        
        
    
    
        yflow
        admin
        8000
        
    

И мы используем кодировку UTF-16 для анализа вышеупомянутого XML-файла в кодировке UTF-8.

  SAXParserFactory factory = SAXParserFactory.newInstance();

  try (InputStream is = getXMLFileAsStream()) {

      SAXParser saxParser = factory.newSAXParser();

      // parse XML and map to object, it works, but not recommend, try JAXB
      MapStaffObjectHandlerSax handler = new MapStaffObjectHandlerSax();

      // more options for configuration
      XMLReader xmlReader = saxParser.getXMLReader();
      xmlReader.setContentHandler(handler);

      InputSource source = new InputSource(is);

      // UTF-16 to parse an UTF-8 XML file
      source.setEncoding(StandardCharsets.UTF_16.toString());
      xmlReader.parse(source);

      // print all
      List result = handler.getResult();
      result.forEach(System.out::println);

  } catch (ParserConfigurationException | SAXException | IOException e) {
      e.printStackTrace();
  }

Выход

[Fatal Error] :1:1: Content is not allowed in prolog.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1243)
at java.xml/com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:635)
at com.mkyong.xml.sax.ReadXmlSaxParser2.main(ReadXmlSaxParser2.java:45)

4. Скачать Исходный Код

$клон git $клон git

$компакт-диск java-xml

$cd src/основной/java/com/mkyong/xml/саксофон/

5. Рекомендации

  • Синтаксический анализатор Java SAX
  • Java добавляет и удаляет спецификацию из файла UTF-8
  • Википедия – Метка порядка байтов (спецификация)
  • В чем разница между UTF-8 и UTF-8 без спецификации?

Оригинал: “https://mkyong.com/java/sax-error-content-is-not-allowed-in-prolog/”



  • Метки



    bom, content, error, text, xml

Problem

When trying to import the library.xml file by a browser or by Application Designer, the following error messages are displayed:

a) by browser, http://:/maximo/webclient/utility/import.jsp

Error 500-Internal Server Error
From RFC 2068 Hypertext Transfer Protocol — HTTP/1.1:
10.5.1.500 Internal Server Error
The server encountered an unexpected condition which prevented it from
fulfulling the request.

b) by Application Designer

BMXAA0931E — Content is not allowed in prolog.

Symptom

BMXAA0931E — Content is not allowed in prolog.

Cause

When you export the library.xml file for editing, the editor you use might corrupt this file.

Environment

All versions of Maximo.

Resolving The Problem

Work with another editor like Wordpad or Eclipse to edit the library.xml file before importing.

[{«Product»:{«code»:»SSLKT6″,»label»:»IBM Maximo Asset Management»},»Business Unit»:{«code»:»BU059″,»label»:»IBM Software w/o TPS»},»Component»:»System Related»,»Platform»:[{«code»:»PF025″,»label»:»Platform Independent»}],»Version»:»7.5;7.6″,»Edition»:»All Editions»,»Line of Business»:{«code»:»LOB59″,»label»:»Sustainability Software»}}]

Понравилась статья? Поделить с друзьями:
  • Cosmo mini 30 ошибка 255
  • Corsa d проверка ошибок
  • Corsa d ошибка c0545
  • Cors ошибка fetch
  • Corrupt disk steam ошибка решение