• Post Reply Bookmark Topic Watch Topic
  • New Topic

Spring, XML error : XML document from class path resource [] is invalid  RSS feed

 
Philip Freeman
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to this and having a hard time understanding this error.

Console output:
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from class path resource [] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.


People at stackoverflow and other forums have already posted this error and the answers are along the lines: BAD XML, look for white space, etc.
I believe it is not so or I at least do not have a clear understanding of the error message.

To find an answer to this problem I have created another project and purposely inserted a few characters before my prolog (first line of xml if I understood that right.)
The error I get in that case is:
  Line 1 in XML document from ServletContext resource [/WEB-INF/classicmodels-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.


The [/WEB-INF/classicmodels-servlet.xml] tells me exactly where it is and what to look from I know fromContent is not allowed in prolog

While here:

XML document from class path resource [] is invalid.  I have no idea what is going on.


Any help or advice would be appreciated. Thanks in advance!




My xml documents:

productsDao-context.xml


productsService-context.xml


cars-servlet.xml


web.xml



pom.xml
 
Tim Nachreiner
Ranch Hand
Posts: 61
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your .xml files reference , but you are using Spring5.  Maybe that is the problem.  I'd try to compile with v4.3 and see if the error goes away.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XML document from class path resource [] is invalid.


Note carefully the name of the resource which is invalid. It's empty, right? Notice that when you experimented with invalid XML resources and caused a similar error message, that message told you the name of the XML resource which you modified. So maybe something is getting a resource (from the class path, not from the ServletContext) but not providing a name for that resource.

It's hard for me to point specifically at anything in your configuration, but the web.xml does seem to have something near the end which looks like it's trying to point at XML documents in the class path. Maybe there's some kind of problem there?

Also I suppose you got this message out of a log file? Perhaps other log file entries just before it might provide some context as to what was happening just then?
 
Tim Nachreiner
Ranch Hand
Posts: 61
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here my web.xml.   I think you are missing the <init-params> that point to the .xml file, hence, the null resource error.



 
Tim Nachreiner
Ranch Hand
Posts: 61
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is how I configured my ContextLoadListener:



Whereas, your files are on your classpath.  Might want to reconsider that.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how Spring handles resources which are relative to the class path, as in Philip's configuration. If it's the same way that the Class.getResource() method works then this:

        classpath: com/modelsclassiccars/beans/productsDao-context.xml


might be looking in the classpath relative to the (Spring) class which is trying to read the XML. And since that class is in a package, that wouldn't work. But (still with my speculative assumptions) using an absolute path might fix the problem:

        classpath: /com/modelsclassiccars/beans/productsDao-context.xml


 
Tim Nachreiner
Ranch Hand
Posts: 61
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's how I think this story begins:  Philip started learning Spring by creating a Spring Java app, and configured his Application Context like this:



with the .xml file residing on the classpath (because there is nowhere else to put it).
Then he tried to cross-pollenate a Spring WebMVC app using the same logic, without fully understanding how to configure a "web Application Context".

According to Spring documentation Spring 4.3.7 , on page 156:


You can create ApplicationContext instances declaratively by using, for example, a
ContextLoader. Of course you can also create ApplicationContext instances programmatically
by using one of the ApplicationContext implementations.
You can register an ApplicationContext using the ContextLoaderListener as follows:





The listener inspects the contextConfigLocation parameter. If the parameter does not exist, the
listener uses /WEB-INF/applicationContext.xml as a default. When the parameter does exist,
the listener separates the String by using predefined delimiters (comma, semicolon and whitespace)
and uses the values as locations where application contexts will be searched. Ant-style path patterns
are supported as well. Examples are /WEB-INF/*Context.xml for all files with names ending with
"Context.xml", residing in the "WEB-INF" directory, and /WEB-INF/**/*Context.xml, for all such
files in any subdirectory of "WEB-INF".


The ContextLoaderListener class in web.xml will bootstrap the "web Application Context" via the .xml file(s) in the WEB-INF folder.

So, Paul is correct saying the .xml path is relative to "which Application Context interface is being implemented", and Philip COULD use the classpath, but Spring documentation implies you should use the WEB-INF folder.


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!