Java 8中的默认SAX解析器

3

我正在努力整理关于Java中SAX/DOM集成的思路。我发现互联网上的信息很混乱。如果有人能回答一些简单的问题,我将不胜感激。

以下是我的环境:

  • Eclipse Oxygen。
  • Java 8。
  • Maven项目没有任何依赖项(是一个简单的控制台测试项目)。

我的问题

  • Java 8自带哪些解析器(SAX/DOM)?我在互联网上找不到相关信息。
  • 从我的环境中删除CLASSPATH后,确保未设置org.xml.sax.driver系统属性,XMLReaderFactory.createXMLReader()方法仍然返回一个对象com.sun.org.apache.xerces.internal.parsers.SAXParser。它来自哪里?

1
你回答了自己的问题:你要求一个没有在classpath中的库的SAX解析器,并获得了一个com.sun.org.apache.xerces.internal.parsers.SAXParser的实例。所以这就是Java 8捆绑的SAX解析器。 - undefined
那并没有真正回答问题。显然,Oracle获取了某个版本的Xerces SAX支持。是哪个版本?使用特定的Xerces实现有什么价值? - undefined
2个回答

1

你在JVM中使用的(默认)Xerces解析器的版本取决于你使用的Java构建版本。正如你可以从https://bugs.openjdk.org/browse/JDK-8282280中看到的,如果需要,OpenJDK团队将会将更新的Xerces版本引入OpenJDK代码树...并将更改回溯到支持的Java版本。

在Oracle知识库中有一篇文章,可以了解如何查找JVM的Xerces版本,但你需要一个Oracle支持账户才能阅读:

显然,在Java 8上,你可以通过运行以下命令来找到这个信息:

$ java com.sun.org.apache.xerces.internal.impl.Version

...但是这在后续版本中不起作用。(也许,有人可以评论提供新的解决方案?)
这个问答也相关,但我不认为它回答了关于JVM内置Xerces实现的“哪个版本”问题:

0

https://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/SAXParserFactory.html

https://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/SAXParserFactory.html#newInstance--

获取一个新的SAXParserFactory实例。这个静态方法创建一个新的工厂实例。该方法使用以下有序查找过程来确定要加载的SAXParserFactory实现类:
Use the javax.xml.parsers.SAXParserFactory system property.
Use the properties file "lib/jaxp.properties" in the JRE directory. This configuration file is in standard java.util.Properties format and contains the fully qualified name of the implementation class with the key being the system property defined above. The jaxp.properties file is read only once by the JAXP implementation and it's values are then cached for future use. If the file does not exist when the first attempt is made to read from it, no further attempts are made to check for its existence. It is not possible to change the value of any property in jaxp.properties after it has been read for the first time.
Use the service-provider loading facilities, defined by the ServiceLoader class, to attempt to locate and load an implementation of the service using the default loading mechanism: the service-provider loading facility will use the current thread's context class loader to attempt to load the service. If the context class loader is null, the system class loader will be used.
Otherwise the system-default implementation is returned.

一旦应用程序获得了对SAXParserFactory的引用,它可以使用工厂来配置和获取解析器实例。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接