在 Java 中引用本地 DTD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6331831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:26:14  来源:igfitidea点击:

Referring to a local DTD in Java

javaxmlsaxdtd

提问by Paul Reiners

I have some XML that I'm parsing with a SAX parser in Java. It starts with this preamble:

我有一些 XML 正在用 Java 中的 SAX 解析器进行解析。它从这个序言开始:

<!DOCTYPE math 
    PUBLIC "-//W3C//DTD MathML 3.0//EN"
           "http://www.w3.org/Math/DTD/mathml3/mathml3.dtd">

How do I change this to use a local DTD?

如何更改它以使用本地 DTD?

I suppose I could do something like this:

我想我可以做这样的事情:

<!DOCTYPE math 
    PUBLIC "-//W3C//DTD MathML 3.0//EN"
           "file:///c:/MathML/mathml3.dtd">

Not exactly like that, but somethinglike that. However, I need the path to be independent of the user's system.

不完全是这样,但事情那样。但是,我需要独立于用户系统的路径。

How do I use a local DTD with a path relative to the class path?

如何使用具有相对于类路径的路径的本地 DTD?

采纳答案by Jim Garrison

Take a look at this articleon using XML catalogs to resolve DTDs locally without having to modify your XML source. The basic steps are:

看看这篇关于使用 XML 目录在本地解析 DTD 而不必修改您的 XML 源的文章。基本步骤是:

  1. create an XML file that maps system IDs to local DTDs
  2. modify your code to instantiate and configure a CatalogResolver
  3. provide the CatalogResolver to the XML Reader (obtained from the parser)
  1. 创建一个将系统 ID 映射到本地 DTD 的 XML 文件
  2. 修改您的代码以实例化和配置 CatalogResolver
  3. 向 XML Reader 提供 CatalogResolver(从解析器获得)

回答by Hitham S. AlQadheeb

When dealing with Web Apps, you can put the dtd in the lib folder and refer to it like:

在处理 Web Apps 时,您可以将 dtd 放在 lib 文件夹中,并像这样引用它:

<!DOCTYPE name PUBLIC 
    "-//CMP//DTD dtdName 1.0//EN"
        "/WEB-INF/lib/dtdName.dtd">

回答by alok

The solution is to provide the DTD file location in the system using classpath. So the DocType that worked offline would be:

解决方案是使用类路径在系统中提供 DTD 文件位置。所以离线工作的 DocType 将是:

<!DOCTYPE hibernate-configuration SYSTEM 
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">

回答by Puneet Akhouri

Also another way can be to keep the dtd at the localhost so that the final path becomes something like:

另一种方法是将 dtd 保留在本地主机上,以便最终路径变为:

<!DOCTYPE hibernate-configuration SYSTEM 
          "http://localhost/hibernate-configuration-3.0.dtd">

Definitely not the most elegant solution but surely does work.

绝对不是最优雅的解决方案,但肯定有效。