Line 23 in XML document from class path resource [spring-mvc.xml] is invalid; 报错解决方法

本文记录了一次将项目转换为Maven架构过程中遇到的XML解析错误,并详细描述了解决该问题的过程,最终确定是由dbcp依赖引起的XML解析冲突。

今天在把自己的项目转为maven架构的时候,居然碰到了一个很奇葩的问题具体如下:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:

  Line 23 in XML document from class path resource [spring-mvc.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cos-all-limited.1.2: An ''all'' model group must appear in a particle with '{'min occurs'}'='{'max occurs'}'=1, and that particle must be part of a pair which constitutes the '{'content type'}' of a complex type definition.

Caused by: org.xml.sax.SAXParseException: cos-all-limited.1.2: An ''all'' model group must appear in a particle with '{'min occurs'}'='{'max occurs'}'=1, and that particle must be part of a pair which constitutes the '{'content type'}' of a complex type definition.

 

因为之前的applicationContext.xml编码问题,然后就以为是这个文档也是编码的问题。百度了很久,最后就差把这个xml文件手打了,依然报的这个错误,真真被弄得是焦头烂额了,编码问题,不太懂啊。

 

然后,觉得是不是jar包导入的有问题(方向对了,但是我犯了个致命的错误),于是,把原来项目里的jar包重新导入到maven生成的war包中,再启动,依然是这个错误,要抓狂了都。

 

其实,正确的方法应该是先把该项目总的jar包删除后,再从原项目导入jar包,这个是肯定不会出问题的。因为是用maven控制的包的导入,只能是在导入的时候出现了包冲突的问题,造成了解析错误,重新查看jar包引入的过程,在 这篇博文中发现,原来是在导入dbcp的jar包时,又引入了一个xml文档解析的jar包,造成了冲突。

正确的引入dbcp依赖的方法是:

  排除依赖包

         <dependency>

            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>xercesImpl</artifactId>
                    <groupId>xerces</groupId>
                </exclusion>
            </exclusions>
        </dependency>

大家共勉啊!!! 

在处理 Spring 配置文件时,如果遇到 `Line 7 in spring-01.xml is invalid XML format` 错误,通常表示 XML 文件的结构存在语法或格式问题。以下是一些常见的排查和解决方法: ### 1. 检查 XML 文件的语法 确保 XML 文件的语法是正确的。常见的错误包括标签未闭合、属性值未用引号包围、标签嵌套错误等。可以使用 XML 验证工具或 IDE 的 XML 验证功能来检查文件的语法。 例如,以下是一个格式错误的 XML 示例: ```xml <bean id="exampleBean" class="com.example.ExampleBean"> <property name="exampleProperty" value="exampleValue" </bean> ``` 在上述代码中,`<property>` 标签没有正确闭合,应修改为: ```xml <bean id="exampleBean" class="com.example.ExampleBean"> <property name="exampleProperty" value="exampleValue"/> </bean> ``` ### 2. 检查命名空间声明 如果配置文件中使用了 Spring 的命名空间(如 `context`、`aop` 等),需要确保命名空间的声明是正确的。错误的命名空间声明可能导致 XML 解析失败。 例如,以下是一个使用 `context:annotation-config` 的正确命名空间声明: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> </beans> ``` ### 3. 检查 XML 文件的编码 确保 XML 文件的编码格式与文件声明的编码一致。通常,XML 文件的编码声明位于文件的开头,如 `<?xml version="1.0" encoding="UTF-8"?>`。如果文件的实际编码与声明的编码不一致,可能会导致解析错误。 ### 4. 使用 IDE 的 XML 验证功能 大多数现代 IDE(如 IntelliJ IDEA、Eclipse)都提供了 XML 验证功能。可以通过 IDE 的功能来检查 XML 文件的格式问题,并根据提示进行修复。 ### 5. 检查 XML 文件的路径 确保 Spring 配置文件的路径是正确的。如果文件路径错误,Spring 容器可能无法找到并解析配置文件,导致 XML 格式错误。 ### 6. 检查 XML 文件的内容 确保 XML 文件的内容没有被意外修改或损坏。可以通过比较文件的历史版本或使用版本控制工具来检查文件内容的完整性。 ### 7. 使用 XML 验证工具 可以使用在线的 XML 验证工具来检查 XML 文件的格式。这些工具通常会提供详细的错误信息,帮助快速定位问题。 ### 示例代码 以下是一个简单的 Spring 配置文件示例,确保其格式正确: ```xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="exampleBean" class="com.example.ExampleBean"> <property name="exampleProperty" value="exampleValue"/> </bean> </beans> ``` 通过以上方法,可以有效地解决 Spring 配置文件中 XML 格式错误的问题。
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值