aspose file tools*
The moose likes Java in General and the fly likes Content is not allowed in prolog (EDIT: emergency code fix help!) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Content is not allowed in prolog (EDIT: emergency code fix help!)" Watch "Content is not allowed in prolog (EDIT: emergency code fix help!)" New topic
Author

Content is not allowed in prolog (EDIT: emergency code fix help!)

James McKee
Greenhorn

Joined: Jul 11, 2007
Posts: 29
I'm parsing through an XML document:

private String filename = "C:\\XML.xml";

private void initialize() {

String xmlData = HelperUtl.readXmlFile( filename );
xmlData = ( XmlDataDcl )xmlObject.parse( xmlData, null, plan );
retrieveCurrentPlan();

}




When running a JUnit test, it throws the following error:

"Call to parse XML Failed"
Cause by: org.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.


I can't figure out why this is happening. Here is the XML file (everything is formatted correctly - I'm using Oxygen)

EDIT: SEE XML BELOW




I've searched for answers on google and other people had this problem because their editor was open, the doc wasn't formatted correctly, or it didn't have this:
<?xml version="1.0" encoding="UTF-8"?>
at the top. i've tried everything, no dice. any help would be appreciated.

[ November 14, 2007: Message edited by: James McKee ]
[ November 14, 2007: Message edited by: James McKee ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Originally posted by James McKee:
Here is the XML file (everything is formatted correctly - I'm using Oxygen)

No it isn't.

You don't close element "Info" but "Rule" instead.
You don't close your root "Data" element.

Now my guess is that "Info" is an element without nested elements, so it should end with "/>", and the second "<Data" should be "<Rule" instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James McKee
Greenhorn

Joined: Jul 11, 2007
Posts: 29
sorry, i just typed all that out quickly as an example. I didn't want to post any true company files
James McKee
Greenhorn

Joined: Jul 11, 2007
Posts: 29
the real file all of the xml is fine
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by James McKee:
the real file all of the xml is fine


Remember that the art of debugging is the art of figuring out which of the things you "know" are actually not true. The possibilities here are 1) your XML parser has a bug (unlikely), or 2) the statement about is untrue (extremely likely.)

Can you just cut and paste the first, say, three lines of the real XML file?


[Jess in Action][AskingGoodQuestions]
James McKee
Greenhorn

Joined: Jul 11, 2007
Posts: 29
Here's the first 7:

<?xml version="1.0" encoding="UTF-8"?>
<Overrides>
<Plan NAME="Term">
<Rule NAME="Anniversary">
<Variable NAME="NewPolicyYear" DATATYPE="INTEGER"/>
</Rule>
</Plan>




(ovverrides is closed at the end of the doc)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

It's possible that there are invisble characters at the top of your XML file. Some editors, for example UltraEdit, insert some special bytes at the top of an UTF-8 file, which can lead to problems like this.

Look at your file with a hex editor, to see exactly what bytes the file contains, and check if there are strange bytes at the beginning of the file.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
James McKee
Greenhorn

Joined: Jul 11, 2007
Posts: 29
just opened the fie with the XVI32U hex editor and I don't see any strange characters...

the XML editor I use is Oxygen
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Maybe you're validating against a schema, and the schema has a different charset declaration than "UTF-8" ? I've heard of folks having problems like this, although strictly it should work just fine.
James McKee
Greenhorn

Joined: Jul 11, 2007
Posts: 29
what other schemas/charsets could I try?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If you're using an .xsd file, then it's possible you'll have trouble unless the first line also says 'encoding="UTF-8"'.
James McKee
Greenhorn

Joined: Jul 11, 2007
Posts: 29
it's a .XML file...
James McKee
Greenhorn

Joined: Jul 11, 2007
Posts: 29
I think I may have found the problem, but I need help implementing the change.

We have an XMLHelper utility, and inside this class there's a method called "xmlFormat". Apparently the line "format.setSuppressDeclaration( true );" will solve my problems. In order to do so, I have to edit the "readXmlFile" method I'm originally calling. Here's what I have to work with:

xmlformat method in XMLHelper utility:






readXMLFile method in File Helper utility:






based on this, how can I implement the code in the xmlFormat method into the readXMLfile method? Thank you
James McKee
Greenhorn

Joined: Jul 11, 2007
Posts: 29
oh no! when I was playing around with the FileHelper utility we have, I accidently deleted the file and now the "openFile" method is gone. Based on the readXmlFile method that calls the openFile, can anyone tell me how to recreate this method? ugh, what a disaster!

James McKee
Greenhorn

Joined: Jul 11, 2007
Posts: 29
please help, I'd greatly appreciate it
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Of course we can't know exactly what the old method did, but, for example



would compile, and likely is close enough to your original -- although perhaps the original added a path to filename or something.

Please back everything up before proceeding!
James McKee
Greenhorn

Joined: Jul 11, 2007
Posts: 29
Thanks. I'll give that a shot. How about my original question about implementing the xmlformat method into the xmlread method?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I didn't actually understand that question, nor do I understand how that readXmlFile() method could ever have formed part of a working system. It reads the file in and appends the Unicode value (an integer) for each character to a String, returning a String made up of those integers. Perhaps that's the whole problem right there, that this method just makes a mess of your data. Write a tiny program that just calls readXmlFile on a file and prints out the String it returns; you'll see it's gibberish, just a random string of digits.
James McKee
Greenhorn

Joined: Jul 11, 2007
Posts: 29
Yeah you're right, I just realized if I were to do something like that anyway it'd be parsing through the XML file twice which is not what I want. I need to figure out what's going on, this error is killing me haha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Content is not allowed in prolog (EDIT: emergency code fix help!)