如何解决XML报错:根元素后标记格式不正确(已按教程闭合代码)
解决XML报错:"The markup in the document following the root element must be well-formed"
这个错误的核心原因是你的XML配置文件违反了XML单根元素规则——XML文档要求所有内容必须被一个唯一的根元素包裹,根元素之外不能有任何同级的XML元素或多余标记。
你虽然正确闭合了<context:component-scan>标签,但大概率是这个标签和其他配置元素(比如<mvc:annotation-driven>、<bean>等)处于同级位置,没有被统一的根元素(比如Spring配置里的<beans>)包裹。
举个正确的结构示例:
<?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"> <!-- 所有配置元素都放在<beans>根元素内部 --> <context:component-scan base-package="DiamonShop"></context:component-scan> <!-- 其他配置标签也嵌套在这里 --> </beans>
排查步骤:
- 打开你的XML配置文件,检查最外层是否有且只有一个根元素(比如Spring的
<beans>) - 确保所有其他
<context:*>、<bean>等标签都完全嵌套在这个根元素内部,没有任何元素出现在根元素的开始标签之前或结束标签之后 - 检查文件末尾是否有多余的空白字符、注释或其他无效标记
内容的提问来源于stack exchange,提问作者Nguyen Cuc
相关产品推荐
相关产品推荐

