0
    foundation.NSPropertyListSerialization$_XML$DictionaryParser.fatalError- Parse fatal error : 
org.xml.sax.SAXParseException: Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1476)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1037)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:513)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:815)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:744)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:128)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1208)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:543)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)

This question has multiple answers - Content is not allowed in Prolog SAXParserException and Java parsing XML document gives "Content not allowed in prolog." error

I am not able to resolve the error despite referring to all the posts possible. I also tried the solution recommended here - http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html

I combined both the answers, and did this -

String inputBytesToStr = new String(inputBytes);
if (inputBytesToStr.startsWith("\uFEFF")) {
            inputBytesToStr = inputBytesToStr.substring(1);             
        }
        inputBytesToStr = inputBytesToStr.trim().replaceFirst("^([\\W]+)<","<");
        inputBytesToStr = inputBytesToStr.replaceAll("[^\\x20-\\x7e\\x0A]", "");

Here is my xml

    <?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<info>
    <key>Name</key>
    <string>Patrick</string>
    <key>Country</key>
    <string>Australia</string>
    <array>
        <string>myImageOne.jpeg</string>
        <string>myImageTwo.jpeg</string>
        <string>myImageOne.jpeg</string>
    </array>
</info>
</plist>

Still no luck. I've been working on it for the past 12 hours. I need to resolve this now. PLEASE help me.

Community
  • 1
  • 1
rickygrimes
  • 2,637
  • 9
  • 46
  • 69
  • 1
    Please show your XML input – Henry May 01 '14 at 06:23
  • 1) what are the characters in the beginning that look like white space? 2) try to specify the encoding when you convert from bytes to string: `new String(inputBytes, "UTF-8")` – Henry May 01 '14 at 06:34
  • If you're always parsing the same file, you could try generate a custom `JAXB` parser with [`xjc.exe`](http://theopentutorials.com/examples/java/jaxb/generate-java-class-from-xml-schema-using-jaxb-xjc-command/), instead of writing your own. – classicjonesynz May 01 '14 at 06:35
  • Not always parsing the same file. – rickygrimes May 01 '14 at 06:42
  • Are you parsing the same xml schema (tree structure)? or different schema's of xml files? (because `xpc.exe` will generate a parser based on a `.xsd` file, if you don't have to reinvent the wheel ... don't). – classicjonesynz May 01 '14 at 06:45
  • If the start of the document is literally ` ` then the content not allowed in prolog is the whitespace before ` – McDowell May 01 '14 at 06:49
  • There is no whitespace. Its come from copy paste. More over in the code, I am trimming the white space. – rickygrimes May 01 '14 at 06:50
  • Are you sure that you're definitely trying to parse this particular file and not another `.plist` file in binary rather than XML format? I'd consider using a dedicated plist parsing library such as https://code.google.com/p/plist/, which can cope with both formats transparently. – Ian Roberts May 01 '14 at 07:10
  • @Ian Roberts - 100% correct. It is a binary plist. How does that change what I am trying to do? Please suggest. – rickygrimes May 01 '14 at 07:13
  • You won't be able to parse a binary plist with an XML parser. Try the library I linked to, which claims to support both types and parses them into java objects mirroring the structure of nested dictionaries/arrays. – Ian Roberts May 01 '14 at 07:16

2 Answers2

0

Given this is the standard Apple .plist file format you may have better luck using a dedicated library designed for plist files such as https://code.google.com/p/plist/ instead of trying to parse it by hand.

Using a library would also have the advantage that it could cope transparently with plist files in the binary serialization format as well as the XML form - the same file extension is used for both and you can't tell which format a particular plist is in until you try opening it.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
0

The only luck I have had parsing plists is with dd-plist: https://github.com/3breadt/dd-plist, and having spent a goodly amount of time trying various options, thought it worth mentioning.

saswanb
  • 1,975
  • 1
  • 17
  • 25