为什么我们可以调用接口org.w3c.dom.Document的方法?
我没有看到任何类实现 org.w3c.dom.Document 接口的方法。那么为什么我们(通常)可以调用该接口的 getDocumentElement 方法来获取根元素呢?
I don't see any class implementing the methods of interface org.w3c.dom.Document. Then why can we (usually) call getDocumentElement method of this interface to get the root element ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
org.w3c.dom.Document
是 XML 规范的一部分,可以由许多不同的库实现。如果您想知道使用了哪个具体实现,请在调用其方法的同一位置尝试。这将告诉您具有这些方法的实现类的名称(或其超类)。
org.w3c.dom.Document
is a part of XML specifications which can be implemented by many different libraries. If you want to know which exact implementation is used, tryat the same place where you call methods on it. That will tell you the name of implementing class that would have those methods (or its superclass would).
org.w3c.dom
包及其类是用于 XML 处理的 Java API (JAXP) 的一部分。它的存在是为了提供 DOM Level 2 Core API。语言绑定的存在只是为了提供可以由各种 DOM 解析器实现的接口。毕竟,不同的解析器将采用不同的技术来维护表示 DOM 的内部数据结构。符合 DOM Core API 的多个 JAXP 解析器可以在 JVM 可用的库中共存。在运行时,仅使用其中之一来解析 XML 文档。
一旦实现 JAXP 的合适 DOM 解析器读取了 XML 文档的内容并填充了其内部结构以使 Document 类的实例可供您使用,您就可以调用该方法。换句话说,DOM 解析器负责在解析 XML 文档后为您提供 Document 对象的实例。
The
org.w3c.dom
package and it's classes is part of the Java API for XML Processing (JAXP). It is present to provide the Java language binding for the DOM Level 2 Core API.The language binding merely exists to provide an interface that can be implemented by various DOM parsers. After all, different parsers will have different techniques to maintain the internal data structures that represent the DOM. Multiple JAXP parsers that comply with the DOM Core API can co-exist in the libraries available to the JVM. At runtime, only one of these will be utilized for parsing XML documents.
You can call the method, once a suitable DOM parser that implements JAXP, has read the contents of an XML document, and has populated it's internal structures to make an instance of the Document class available to you. In other words, the DOM parser is responsible for providing you with an instance of the Document object, after parsing a XML document.
Xerces 和 JDom 是少数已知的实现
Few of the known implementations are Xerces and JDom