【百分求助】spring里面的各种xml文件时由拿个文件进行准确性校验或者说进行格式解析的?

wipe_tear 2014-01-26 11:02:47
【百分求助】spring里面的各种xml文件时由拿个文件进行准确性校验或者说进行格式解析的?

1、比如说applicationContext.xml文件,当我写错的时候会有红X提示错误,我想知道这个校验文件在哪里?
2、这个校验文件肯定不是连网通过URL去校验的,因为我关闭电脑的网络连接新建一个项目,仍然可以进行这种校验
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

</beans>


也就是说红色部分,是什么意思,指向哪里?
...全文
567 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
tony4geek 2014-01-28
  • 打赏
  • 举报
回复
schemas xsd 有 校验的。
wipe_tear 2014-01-28
  • 打赏
  • 举报
回复
。。。
引用 14 楼 SDN_SUPERUSER 的回复:
[quote=引用 12 楼 wipe_tear 的回复:] 这个我知道,我是想问问,当我们没有运行spring工程的时候,仅仅是用eclipse编译时候,编译器就会自动知道缩写的xml文件是否正确,我想知道这个机制是怎么做到的 也就是说eclipse这种开发工具,是如何自动加载xsd,去验证我正在编写的xml文件的, 记住是编码的时候自动验证的机制是什么? [quote=引用 11 楼 bao110908 的回复:] 问这个问题,可以说明对于 XML 的解析和 Schema 验证的知识很差啊 这些 xsd 文件在 spring 的 jar 包中存在的,并不需要从网络上获取。而且并没有人规定 xsd 必须从网络中获取的。
[/quote] 这就是IDE干的事。就好像你写JAVA代码的时候,为什么有错误IDE会提示一样[/quote]
jackson_fighting 2014-01-28
  • 打赏
  • 举报
回复
引用 12 楼 wipe_tear 的回复:
这个我知道,我是想问问,当我们没有运行spring工程的时候,仅仅是用eclipse编译时候,编译器就会自动知道缩写的xml文件是否正确,我想知道这个机制是怎么做到的 也就是说eclipse这种开发工具,是如何自动加载xsd,去验证我正在编写的xml文件的, 记住是编码的时候自动验证的机制是什么? [quote=引用 11 楼 bao110908 的回复:] 问这个问题,可以说明对于 XML 的解析和 Schema 验证的知识很差啊 这些 xsd 文件在 spring 的 jar 包中存在的,并不需要从网络上获取。而且并没有人规定 xsd 必须从网络中获取的。
[/quote] 这就是IDE干的事。就好像你写JAVA代码的时候,为什么有错误IDE会提示一样
wipe_tear 2014-01-28
  • 打赏
  • 举报
回复
自己顶一下,哈哈
wipe_tear 2014-01-27
  • 打赏
  • 举报
回复
这个我知道,我是想问问,当我们没有运行spring工程的时候,仅仅是用eclipse编译时候,编译器就会自动知道缩写的xml文件是否正确,我想知道这个机制是怎么做到的 也就是说eclipse这种开发工具,是如何自动加载xsd,去验证我正在编写的xml文件的, 记住是编码的时候自动验证的机制是什么?
引用 11 楼 bao110908 的回复:
问这个问题,可以说明对于 XML 的解析和 Schema 验证的知识很差啊 这些 xsd 文件在 spring 的 jar 包中存在的,并不需要从网络上获取。而且并没有人规定 xsd 必须从网络中获取的。
Defonds 2014-01-26
  • 打赏
  • 举报
回复
是 xsd 文件。 这个文件在 spring 的 jar 包里边。 spring 的 jar 包里,在 META-IN F目录下都有一个 spring.schemas,它定义的就是用以校验的 xsd 文件的路径。xsd 一般和 class 文件放在一块。 你如果想调整它的校验,可以先编辑相应的 xsd,然后将原来的覆盖,重新打包。
  • 打赏
  • 举报
回复
官网提供的肯定有文档。
wipe_tear 2014-01-26
  • 打赏
  • 举报
回复
引用 3 楼 fangmingshijie 的回复:
http://www.w3school.com.cn/dtd/dtd_validation.asp 我感觉应该是这样的。
举个例子,比如我现在在官网上下载了spring4.0 M2 的各种jar包 想开发基于spring4.0的程序 但是我现在根本不知道这两个spring的配置文件应该如何开头 applicationContext.xml spring-servlet.xml 我总不能拿3.1的配置文件去写吧,虽然能成功,但那也仅仅是因为向下兼容的原因呀
  • 打赏
  • 举报
回复
http://www.w3school.com.cn/dtd/dtd_validation.asp 我感觉应该是这样的。
wipe_tear 2014-01-26
  • 打赏
  • 举报
回复
XSD是什么,另外我没有找到DTD文件啊 在jar包里面吗?
  • 打赏
  • 举报
回复
应该是dtd文件
  • 打赏
  • 举报
回复
问这个问题,可以说明对于 XML 的解析和 Schema 验证的知识很差啊 这些 xsd 文件在 spring 的 jar 包中存在的,并不需要从网络上获取。而且并没有人规定 xsd 必须从网络中获取的。
coolbamboo2008 2014-01-26
  • 打赏
  • 举报
回复
看见这么多高人相互解答啊 好像eclipse工具里可以把xsd文件导入,作为xml文件的验证
wipe_tear 2014-01-26
  • 打赏
  • 举报
回复
你好,谢谢你,不过我有一点不明白,缺少基础知识的支撑 就是: 我们在编写applicationContext的文件的时候,编译器是如何判断语法是否对还是错呢, 加入到build path之后就会classloader到内存,自动检查吗, 也就是原理是什么,不知道您能否明白我的意思
引用 8 楼 defonds 的回复:
是的。自动加载、解析。 我们可以看一下 spring-beans-xxx.jar 中的对象工厂中 PluggableSchemaResolver 关于此的一些源码片段:
/***
	 * Loads the schema URL -> schema file location mappings using the default
	 * mapping file pattern "META-INF/spring.schemas".
	 * @param classLoader the ClassLoader to use for loading
	 * (can be <code>null</code>) to use the default ClassLoader)
	 * @see PropertiesLoaderUtils#loadAllProperties(String, ClassLoader)
	 */
	public PluggableSchemaResolver(ClassLoader classLoader) {
		this.classLoader = classLoader;
		this.schemaMappingsLocation = DEFAULT_SCHEMA_MAPPINGS_LOCATION;
	}

	/***
	 * Loads the schema URL -> schema file location mappings using the given
	 * mapping file pattern.
	 * @param classLoader the ClassLoader to use for loading
	 * (can be <code>null</code>) to use the default ClassLoader)
	 * @param schemaMappingsLocation the location of the file that defines schema mappings
	 * (must not be empty)
	 * @see PropertiesLoaderUtils#loadAllProperties(String, ClassLoader)
	 */
	public PluggableSchemaResolver(ClassLoader classLoader, String schemaMappingsLocation) {
		Assert.hasText(schemaMappingsLocation, "'schemaMappingsLocation' must not be empty");
		this.classLoader = classLoader;
		this.schemaMappingsLocation = schemaMappingsLocation;
	}

	public InputSource resolveEntity(String publicId, String systemId) throws IOException {
		if (logger.isTraceEnabled()) {
			logger.trace("Trying to resolve XML entity with public id [" + publicId +
					"] and system id [" + systemId + "]");
		}

		if (systemId != null) {
			String resourceLocation = getSchemaMappings().get(systemId);
			if (resourceLocation != null) {
				Resource resource = new ClassPathResource(resourceLocation, this.classLoader);
				try {
					InputSource source = new InputSource(resource.getInputStream());
					source.setPublicId(publicId);
					source.setSystemId(systemId);
					if (logger.isDebugEnabled()) {
						logger.debug("Found XML schema [" + systemId + "] in classpath: " + resourceLocation);
					}
					return source;
				}
				catch (FileNotFoundException ex) {
					if (logger.isDebugEnabled()) {
						logger.debug("Couldn't find XML schema [" + systemId + "]: " + resource, ex);
					}
				}
			}
		}
		return null;
	}

	/***
	 * Load the specified schema mappings lazily.
	 */
	private Map<String, String> getSchemaMappings() {
		if (this.schemaMappings == null) {
			synchronized (this) {
				if (this.schemaMappings == null) {
					if (logger.isDebugEnabled()) {
						logger.debug("Loading schema mappings from [" + this.schemaMappingsLocation + "]");
					}
					try {
						Properties mappings =
								PropertiesLoaderUtils.loadAllProperties(this.schemaMappingsLocation, this.classLoader);
						if (logger.isDebugEnabled()) {
							logger.debug("Loaded schema mappings: " + mappings);
						}
						Map<String, String> schemaMappings = new ConcurrentHashMap<String, String>();
						CollectionUtils.mergePropertiesIntoMap(mappings, schemaMappings);
						this.schemaMappings = schemaMappings;
					}
					catch (IOException ex) {
						throw new IllegalStateException(
								"Unable to load schema mappings from location [" + this.schemaMappingsLocation + "]", ex);
					}
				}
			}
		}
		return this.schemaMappings;
	}
另,spring4 不了解,不做解释。
Defonds 2014-01-26
  • 打赏
  • 举报
回复
是的。自动加载、解析。

我们可以看一下 spring-beans-xxx.jar 中的对象工厂中 PluggableSchemaResolver 关于此的一些源码片段:
/***
* Loads the schema URL -> schema file location mappings using the default
* mapping file pattern "META-INF/spring.schemas".
* @param classLoader the ClassLoader to use for loading
* (can be <code>null</code>) to use the default ClassLoader)
* @see PropertiesLoaderUtils#loadAllProperties(String, ClassLoader)
*/
public PluggableSchemaResolver(ClassLoader classLoader) {
this.classLoader = classLoader;
this.schemaMappingsLocation = DEFAULT_SCHEMA_MAPPINGS_LOCATION;
}

/***
* Loads the schema URL -> schema file location mappings using the given
* mapping file pattern.
* @param classLoader the ClassLoader to use for loading
* (can be <code>null</code>) to use the default ClassLoader)
* @param schemaMappingsLocation the location of the file that defines schema mappings
* (must not be empty)
* @see PropertiesLoaderUtils#loadAllProperties(String, ClassLoader)
*/
public PluggableSchemaResolver(ClassLoader classLoader, String schemaMappingsLocation) {
Assert.hasText(schemaMappingsLocation, "'schemaMappingsLocation' must not be empty");
this.classLoader = classLoader;
this.schemaMappingsLocation = schemaMappingsLocation;
}

public InputSource resolveEntity(String publicId, String systemId) throws IOException {
if (logger.isTraceEnabled()) {
logger.trace("Trying to resolve XML entity with public id [" + publicId +
"] and system id [" + systemId + "]");
}

if (systemId != null) {
String resourceLocation = getSchemaMappings().get(systemId);
if (resourceLocation != null) {
Resource resource = new ClassPathResource(resourceLocation, this.classLoader);
try {
InputSource source = new InputSource(resource.getInputStream());
source.setPublicId(publicId);
source.setSystemId(systemId);
if (logger.isDebugEnabled()) {
logger.debug("Found XML schema [" + systemId + "] in classpath: " + resourceLocation);
}
return source;
}
catch (FileNotFoundException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Couldn't find XML schema [" + systemId + "]: " + resource, ex);
}
}
}
}
return null;
}

/***
* Load the specified schema mappings lazily.
*/
private Map<String, String> getSchemaMappings() {
if (this.schemaMappings == null) {
synchronized (this) {
if (this.schemaMappings == null) {
if (logger.isDebugEnabled()) {
logger.debug("Loading schema mappings from [" + this.schemaMappingsLocation + "]");
}
try {
Properties mappings =
PropertiesLoaderUtils.loadAllProperties(this.schemaMappingsLocation, this.classLoader);
if (logger.isDebugEnabled()) {
logger.debug("Loaded schema mappings: " + mappings);
}
Map<String, String> schemaMappings = new ConcurrentHashMap<String, String>();
CollectionUtils.mergePropertiesIntoMap(mappings, schemaMappings);
this.schemaMappings = schemaMappings;
}
catch (IOException ex) {
throw new IllegalStateException(
"Unable to load schema mappings from location [" + this.schemaMappingsLocation + "]", ex);
}
}
}
}
return this.schemaMappings;
}


另,spring4 不了解,不做解释。
wipe_tear 2014-01-26
  • 打赏
  • 举报
回复
引用 6 楼 defonds 的回复:
是 xsd 文件。 这个文件在 spring 的 jar 包里边。 spring 的 jar 包里,在 META-IN F目录下都有一个 spring.schemas,它定义的就是用以校验的 xsd 文件的路径。xsd 一般和 class 文件放在一块。 你如果想调整它的校验,可以先编辑相应的 xsd,然后将原来的覆盖,重新打包。
只要把jar导入到build path,就会自动解析xml文件,是吗? 另外基于spring4的配置文件如何配置,我在官网找了半天,没找到,能否给个思路?

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧