Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
James McKee
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James McKee
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, i just typed all that out quickly as an example. I didn't want to post any true company files
 
James McKee
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the real file all of the xml is fine
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
James McKee
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James McKee
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what other schemas/charsets could I try?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's a .XML file...
 
James McKee
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help, I'd greatly appreciate it
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic