分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

在Spring中最简单的一个xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC  "-//SPRING//DTD BEAN//EN"  "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
        <bean id="hello" class="com.test.Hello" singleton="true"/>
</beans>
但是这里的DTD使用的是网络路径,如果电脑没有上网的话,那么Spring读取这个XML就一定会出问题,因为无法到达这个网络地址,解决方法就是使用本地的DTD,那么路径应该如何设置呢?一般我们的配置文件都是放在classes目录下的,也就是在Classpath下,那么是不是要把spring-beans.dtd放到classes里面呢?不行,来看一下Spring中org.springframework.beans.factory.xml.BeansDtdResolver这个类的源代码:
/*
 * Copyright 2002-2004 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.beans.factory.xml;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
 * EntityResolver implementation for the Spring beans DTD,
 * to load the DTD from the Spring classpath resp. JAR file.
 *
 * <p>Fetches "spring-beans.dtd" from the classpath resource
 * "/org/springframework/beans/factory/xml/spring-beans.dtd",
 * no matter if specified as some local URL or as
 * "http://www.springframework.org/dtd/spring-beans.dtd".
 *
 * @author Juergen Hoeller
 * @since 04.06.2003
 */
public class BeansDtdResolver implements EntityResolver {
 private static final String DTD_NAME = "spring-beans";
 private static final String SEARCH_PACKAGE = "/org/springframework/beans/factory/xml/";
 protected final Log logger = LogFactory.getLog(getClass());
 public InputSource resolveEntity(String publicId, String systemId) throws IOException {
  logger.debug("Trying to resolve XML entity with public ID [" + publicId +
         "] and system ID [" + systemId + "]");
  if (systemId != null && systemId.indexOf(DTD_NAME) > systemId.lastIndexOf("/")) {
   String dtdFile = systemId.substring(systemId.indexOf(DTD_NAME));
   logger.debug("Trying to locate [" + dtdFile + "] under [" + SEARCH_PACKAGE + "]");
   try {
    Resource resource = new ClassPathResource(SEARCH_PACKAGE + dtdFile, getClass());
    InputSource source = new InputSource(resource.getInputStream());
    source.setPublicId(publicId);
    source.setSystemId(systemId);
    logger.debug("Found beans DTD [" + systemId + "] in classpath");
    return source;
   }
   catch (IOException ex) {
    logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in classpath", ex);
   }
  }
  // use the default behaviour -> download from website or wherever
  return null;
 }
}
很清楚Spring已经把/org/springframework/beans/factory/xml/这个路径写死到代码里了,所以我们也只能把spring-beans.dtd放到这个目录下,才可以保证被xml解析引擎解析到,当然这里也可以使用绝对路径,但是这样的话,就很难在移植了,所以还是使用相对路径比较好,而且Spring已经把这个spring-beans.dtd打包到spring.jar文件中了。
 
好,现在已经确定是使用相对路径了,那么相对路径应该怎么写呢?看之前要搞清楚Spring用的是什么解析引擎,由于Spring使用的是JAXP解析XML文件的,在不做特殊声明的情况下,JDK会使用Crimson解析引擎,具体见Robbin的分析:http://forum.javaeye.com/viewtopic.php?t=75
好了,现在就来看一下Crimson这个解析引擎,在解析DTD时候的代码:
private String resolveURI(String uri)
        throws SAXException
    {
        int     temp = uri.indexOf (':');
        // resolve relative URIs ... must do it here since
        // it's relative to the source file holding the URI!
        // "new java.net.URL (URL, string)" conforms to RFC 1630,
        // but we can't use that except when the URI is a URL.
        // The entity resolver is allowed to handle URIs that are
        // not URLs, so we pass URIs through with scheme intact
        if (temp == -1 || uri.indexOf ('/') < temp) {
            String      baseURI;
            baseURI = in.getSystemId ();
            if (baseURI == null)
                fatal ("P-055", new Object [] { uri });
            if (uri.length () == 0)
                uri = ".";
            baseURI = baseURI.substring (0, baseURI.lastIndexOf ('/') + 1);
            if (uri.charAt (0) != '/')
                uri = baseURI + uri;
            else {
                // We have relative URI that begins with a '/'
                // Extract scheme including colon from baseURI
                String baseURIScheme;
                int colonIndex = baseURI.indexOf(':');
                if (colonIndex == -1) {
                    // Base URI does not have a scheme so default to
                    // "file:" scheme
                    baseURIScheme = "file:";
                } else {
                    baseURIScheme = baseURI.substring(0, colonIndex + 1);
                }
                uri = baseURIScheme + uri;
            }
            // letting other code map any "/xxx/../" or "/./" to "/",
            // since all URIs must handle it the same.
        }
        // check for fragment ID in URI
        if (uri.indexOf ('#') != -1)
            error ("P-056", new Object [] { uri });
        return uri;
    }
由此可知路径里面必须包含":" 和"/"而且":"还必须在"/"的前面,所以这个路径我们可以最简单的写成 ":/spring-beans.dtd" 就可以了。当然":"前面在加上其他的什么路径都不影响解析。

Spring的XML解析中关于DTD的路径问题-

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

Spring的XML解析中关于DTD的路径问题-相关推荐

  1. spring xercesImpl xml 解析问题

    spring xercesImpl xml 解析问题 分类: spring 2009-12-29 16:11 1248人阅读 评论(0) 收藏 举报 xmlspringschema文档jbossjdk ...

  2. Spring的xml配置文件中tx命名空间

    Spring的xml配置文件中tx命名空间 一,spring配置文件的tx命名空间 引入tx命名空间 <?xml version="1.0" encoding="U ...

  3. Spring OXM- 漫谈XML解析技术

    概述 XML解析技术漫谈 认识XML XMl的处理技术 概述 我们先从XML各种解析技术的发展历程谈起,并介绍一些主流 O/X Mapping组件的使用方法,比如XStream.Castor.JiBX ...

  4. Spring之XML解析

    XML解析,我们可以通过我们常用的以下代码作为入口 也许,我们习惯使用第一种加载方式,但是以前也存在 第二种加载,并且这两种加载也有差别,下面再来分析. 先分析 第二种 使用 BeanFactory ...

  5. XML解析中的namespace初探

    初学者在解析XML文件的时候最容易遇到的问题恐怕就是XML的namespace了,本文旨在对namespace做一个简要的介绍. namespace的意义无需多说,和C++,C#等高级语言一样,XML ...

  6. Spring 在xml文件中配置Bean

    Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 < ...

  7. Spring的XML解析原理,java接口流程图

    前言 爱因斯坦说过"耐心和恒心总会得到报酬的",我也一直把这句话当做自己的座右铭,这句箴言在今年也彻底在"我"身上实现了. 每一个程序员都拥有一座大厂梦,我也不 ...

  8. Spring的XML解析原理,java软件开发面试常见问题

    前言 很多同学想进大厂,特别是刚毕业的,以及工作年限短的,不要有任何侥幸心理,踏踏实实的把基础弄扎实了,这是你通往高薪之路的唯一正确姿势. 首先从面试题做起~好了,不多说了,直接上正菜. 在这里分享一 ...

  9. Spring的XML解析原理,ie浏览器java插件下载

    前言 Dubbo用起来就和EJB.WebService差不多,调用一个远程的服务(或者JavaBean)的时候在本地有一个接口,就像调用本地的方法一样去调用,它底层帮你实现好你的方法参数传输和远程服务 ...

最新文章

  1. mysql8.0_grant改变-You are not allowed to create a user with GRANT
  2. python编程中的if __name__ == 'main': 的作用和原理[2]
  3. LeetCode 289. 生命游戏(位运算)
  4. 阮一峰的学习Javascript闭包(Closure)
  5. springboot配置跨mapper.xml的全局变量
  6. webrtc静音检测
  7. PHP无法使用file_get_contents或者curl_init()函数解决办法
  8. HTML5+CSS——个人在线简历
  9. ECharts快速上手 入门教学
  10. 【读书笔记】向上-张自豪:清华学霸的成长之路
  11. kali开机密码破解
  12. 部署ISA2006标准版防火墙
  13. 15/18位身份证号码验证的正则表达式总结
  14. 2018-10-20-C#-从零开始写-SharpDx-应用-初始化dx修改颜色
  15. ubuntu打开浏览器无法上网的问题解决方法?
  16. 打蚊子表情包_打蚊子表情包 - 打蚊子微信表情包 - 打蚊子QQ表情包 - 发表情 fabiaoqing.com...
  17. 9款红包封面来了,定好闹钟领取!
  18. 我们已经走得太远,忘记了为什么出发
  19. 标准化(Normalization)
  20. 我失业了?| ChatGPT生信分析初体验

热门文章

  1. 乐高式微服务化改造(下)
  2. 梯度值与参数更新optimizer.zero_grad(),loss.backward、和optimizer.step()、lr_scheduler.step原理解析
  3. 凸函数高维性质证明(Jenson不等式)
  4. 2019团体程序设计天梯赛L1 L1-1 PTA使我精神焕发L1-2 6翻了L1-3 敲笨钟L1-4 心理阴影面积L1-5 新胖子公式L1-6 幸运彩票L1-7 吃鱼还是吃肉
  5. 基于cnn的图像二分类算法(一)
  6. C++程序设计实践里面石头剪刀布版王者农药实例
  7. 用MOBA游戏的方式来评估候选人实力
  8. ECMAScript6语法检查规范错误信息说明
  9. 一个未知的项目被声明为你的MXML文件的根。切换到源代码模式加以纠正。
  10. 前端学习——这十本书一定要看