8

I've been using Spring JDBC with great success but I am having alot of trouble with this project. I'll post the code links here(it's just a small and silly project to test if I can get it up and running so that I can use Hibernate in the future):

xml-file: http://codepaste.net/uw19zc

main-file: http://codepaste.net/iks1cp

I get tons of errors such as

[Fatal Error] bean2.out.xml:1:1: Premature end of file.
13:21:39,471 FATAL [main] Main  - getAssociatedStylesheets failed

and I haven't created a.out.xml file.

1
  • 1
    post the xml and error that you are getting in your question please Commented Oct 20, 2011 at 11:41

3 Answers 3

45

This error is due to incorrect parsing of the xml file. Using Eclipse to validate it gives the error:

cvc-complex-type.2.3: Element 'beans' cannot have character [children], because the type's content type is element-only.

There appears to be some strange character in between one or many of those <bean> declarations. Have you copied this text from somewhere else?

Remove all spaces and newline characters between <bean> definitions and put them back with your editor.

UPDATE Copying & pasting into notepad++ the text in the codepaste you provided, and setting the charset to UTF-8 showed these characters in the blank lines: xA0. This is the standard Unicode translation for &nbsp;. This is likely to be the cause of this problem.

This validates ok for me:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd">
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/HibernateDB" />
        <property name="username" value="HibernateDB" />
        <property name="password" value="java" />
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        <property name="annotatedClasses">
            <list>
                <value>hdao.HibernateObject</value>
            </list>
        </property>
    </bean><bean id="springHibernateOperator" class="hdao.SpringHibernateOperatorImplementation">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>
Sign up to request clarification or add additional context in comments.

4 Comments

Great answer! Although now something strange happened. When my main method tries to instatiate ClassPathXmlApplicationContext i get exceptions: Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [bean2.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/persistence/Cacheable I do not understand why, haven't i written the xml-file correctly?
The same error is discussed here. Make sure you've got the hibernate-jpa-2.0-api-1.0.0.Final jar in your classpath/maven dependencies. This class is also present in the Java EE 6 api jar. Also, you might want to accept this answer and post another question regarding this matter.
You are correct, I solved it but I will accept the answer, thanks =)
Same problem, opening your file in a good XML editor will quickly point out where your bad character is
7

If you are in linux, use cat -v file-name.xml to detect special "invisible" characters like 'M-BM-' in your xml file

Comments

2

Sometimes there are hidden characters in dependencies or some bean definitions, which you copy from some tutorial's website. best way to find out those hidden characters do a `

ctrl + shift + F

this will format your document and you can see that hidden character in between some tag `

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.