Introduction
This is an article discussing an error appear upon executing a Java Spring Application. The base for the application itself exist in article with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’ in this link. Upon executing the application from a NetBeans IDE, The error appear as is in the following output :
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 23 in XML document from class path resource [spring-config.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 16; cvc-complex-type.2.3: Element 'bean' cannot have character [children], because the type's content type is element-only. at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:402) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:338) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:257) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:671) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85) at com.test.spring.SpringMain.main(SpringMain.java:17) Caused by: org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 16; cvc-complex-type.2.3: Element 'bean' cannot have character [children], because the type's content type is element-only. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:284) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:452) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3230) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidComplexType(XMLSchemaValidator.java:3193) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidType(XMLSchemaValidator.java:3153) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processElementContent(XMLSchemaValidator.java:3055) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2134) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:853) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2967) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339) at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:77) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocument(XmlBeanDefinitionReader.java:432) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390) ... 14 more ------------------------------------------------------------------------ BUILD FAILURE ------------------------------------------------------------------------ Total time: 1.700 s Finished at: 2021-09-09T13:16:22+07:00 ------------------------------------------------------------------------ Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project spring: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1] To see the full stack trace of the errors, re-run Maven with the -e switch. Re-run Maven using the -X switch to enable full debug logging. For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Actually, the error exist in a more simplification form as follows :
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 23 in XML document from class path resource [spring-config.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 16; cvc-complex-type.2.3: Element 'bean' cannot have character [children], because the type's content type is element-only.
Another one appear as follows :
Caused by: org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 16; cvc-complex-type.2.3: Element 'bean' cannot have character [children], because the type's content type is element-only.
So, the article will discuss about how to solve the problem.
Solution
This is actually quite simple to solve the error message appear above. The problem for solving the error message already exist in the error message itself. The hint is in the following line :
Line 23 in XML document from class path resource [spring-config.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 16; cvc-complex-type.2.3: Element 'bean' cannot have character [children], because the type's content type is element-only.
So, the problem exist in line 23 in the Java Spring configuration file with the name of ‘spring-config.xml’. In order to solve the problem, just check the current condition of the ‘spring-config.xml’ file as in the following output :
<?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-3.0.xsd"> <bean id="employeeBean" class="com.test.spring.Employee"> <!-- <property name="id" /> --> <property name="id" value="Employee of the Company" /> <property name="employeeTraining" ref="employeeTrainingBean" /> <property name="employeeSkillList"> <list> <value>Java Programming Language</value> <value>MySQL Database Administration</value> <value>Cisco Routing and Switching Administration</value> <value>Image Editing with Adobe Photoshop</value> </list> </property> --> <constructor-arg ref="employeeTaskBean" /> </bean> </beans>
Actually, line 23 point out to the closing tag of bean or ‘</bean>’. But the problem does not exist at that line. Further up ahead there is a closing remark or comment tag without any opening remark or comment tag. That is the one triggering the error. The main conclusion to this error is if there is ‘XML document from class path resource [xml_file_name.xml] is invalid’, there must be something wrong to the XML form. The additional error is ‘nested exception is org.xml.sax.SAXParseException;’. It means, the XML file parsing process generating an exception. The XML file parsing process ends in failure because of the form, structure of the XML file is wrong or invalid. By removing the wrong or invalid part, it will solve the problem. In the above example or context, it is just by removing the ‘–>’ or the closing remark or commentary tag.
How does the error relate to the ‘bean’ ?. Well, the explanation is that inside the bean tag, the character ‘–>’ does not allowed inside the bean tag. So, the ‘bean’ cannot have character ‘–>’ as its [children].