Java 6 中的 xerces 序列化

发布于 2024-09-02 22:41:49 字数 594 浏览 5 评论 0原文

在 Java 6 中,整个 xerces XML 解析器/序列化器实现现在位于 Java 运行时 (rt.jar) 中。这些包已移至 com.sun.* 命名空间下,这使得它们无法在客户端代码中进行显式引用。使用解析器时这不是问题,解析器是通过 javax API 定义的工厂实例化的。

但是,我们的代码还使用 xerces 序列化 (org.apache.xml.serialize.* )。 AFAICT,没有 javax.xml API 定义的工厂用于创建 Serializer 和 OutputFormat 的实例。这似乎意味着获得一个的唯一方法是显式调用 com.sun.org.apache.xml.serialize.* API。

我在 javax.xml.stream 中找到了序列化类,但它们似乎没有提供任何输出格式控制,如 xerces OutputFormat 类。

问题:

有没有一种方法可以通过 javax 标准 API 访问 xerces 序列化功能(位于 rt.jar 中),而不包含 xerces.jar,也无需显式实例化com.sun.* 类?

如果没有,是否有符合javax API的方法来达到相同的效果?

In Java 6, the entire xerces XML parser/serializer implementation is now in the Java runtime (rt.jar). The packages have been moved under the com.sun.* namespace, which places them off-limits for explicit reference within client code. This is not a problem when using the parser, which is instantiated via javax API-defined factories.

However, our code also uses xerces serialization (org.apache.xml.serialize.* ). AFAICT, there are no javax.xml API-defined factories for creating instances of Serializer and OutputFormat. This seems to imply that the only way to get one is to explicitly call the com.sun.org.apache.xml.serialize.* APIs.

I've found the serialization classes in javax.xml.stream, but they don't seem to provide any output-formatting control like the xerces OutputFormat class.

Question:

Is there a way to access the xerces serialization functionality (which is in rt.jar) via a javax standard API, without including xerces.jar and also without explicitly instantiating com.sun.* classes?

If not, is there an javax API-compliant way to achieve the same effect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落日海湾 2024-09-09 22:41:49

如果您想使用未通过官方 java.*javax.* API 公开的 Xerces 功能,那么您唯一真正的解决方案是将 Xerces 包含为第三方 -党库。以任何方式访问 Xerces 的 JRE 内部版本都是一件危险的事情,因为没有任何东西可以保证有效的 JRE 甚至具有这些类(备用 JRE 甚至同一 JRE 的其他版本可能使用不同的实现来实现 JAXP API,甚至只需将其移动到另一个包中即可)。

If you want to use a Xerces feature that's not exposed through an official java.* or javax.* API, then your only real solution is to include Xerces as a third-party library. Accessing the JRE-internal version of Xerces in any way would be a dangerous thing, since nothing guarantees that a valid JRE even has those classes (alternate JREs and even other versions of the same JRE might implement the JAXP APIs using a different implementation or even just move it to another package).

゛清羽墨安 2024-09-09 22:41:49

据我所知,没有官方 API 可以做到这一点。奇怪的是,Java XML API 仅用于解析 XML。

不过,您可以使用 XML Transformation API 将 DOM Document 写入文件。请参阅此示例

As far as I know there is no official API to do this. The Java XML APIs strangely are only for parsing XML.

You could, however, use the XML Transformation API to write a DOM Document to a file. See this example.

阪姬 2024-09-09 22:41:49

如果您不想引用sun API,您可以将Xerces 放在endorsed 目录中。它将取代 sun 副本/实现,但随后您可以无忧无虑地使用 API(这是“官方”方式)。

Java 内置实现无法对输出提供良好的控制,尽管可以使用 Xerces 和 API 属性对输出进行更好的控制,因为 API 支持传递其他实现可能会觉得有用的附加属性。我自己还没有尝试过最后一个(我只是参考文档)。

编辑(回应评论):如果您想在不控制底层 JDK 的环境中使用 Xerces,以至于您可以为 JAXP 指定自己的替换 API,那么您必须直接引用 Xerces(或引用sun 包重写)。

如果您可以将 Xerces 放在认可的目录中(或者以其他方式覆盖认可的设置 - 坦率地说,这在应用程序服务器中似乎不太可能,尽管我具体不了解 Weblogic),将属性设置为底层实现的“JDK”方法是通过TransformerFactory.setAttribute,根据实现的不同,它可能与 Transformer.setParameter

我应该补充一点,如果sun捆绑的底层版本足够了,并且如果Weblogic使用它(或者使用它自己的Xerces,如果足够的话)那么你可能在这里很幸运,并且能够传递属性并让它工作。

If you don't want to reference sun API's, you can place Xerces in the endorsed directory. It will replace the sun copy/implementation, but then you can use the API without worries (this is the "official" way to do it).

The Java built-in implementation does not provide good control over the output, although it is possible to gain some better control over the output using Xerces and the API properties, since the API supports passing additional properties that other implementations may find useful. Haven't tried that last one myself (I'm only going by the documentation).

Edit (in response to comment): If you want to use Xerces in an enviornment where you do not control the underlying JDK to the point where you can specify your own replacement APIs for JAXP, then you will have to reference Xerces directly (or reference the sun package rewrite).

If you can put Xerces in the endorsed directory (or otherwise override the endorsed setting - which seems unlikely in an app server frankly, although I don't know Weblogic specifically) the "JDK" way of setting the properties to an underlying implementation is via TransformerFactory.setAttribute, which depending on the implementation may interact with Transformer.setParameter.

I should add that should the underlying version that sun bundles be enough, and if Weblogic uses it (or uses its own Xerces if that is enough) then you may be in luck here, and just be able to pass the properties and have it work.

筱果果 2024-09-09 22:41:49

javax.xml.bind.JAXBContext?如果您尝试将对象绑定/序列化到 XML,JAXB 是合适的标准。如果您正在进行原始解析,org.xml.sax 和/或 org.w3c.dom 应该有您正在寻找的内容。

更新: com.javax.transform 包应该会有所帮助。在此处查看 Transformer 示例。

javax.xml.bind.JAXBContext? If you are trying to bind/serialize objects to XML, JAXB is the standard to go with. If you are doing raw parsing, org.xml.sax and/or org.w3c.dom should have what you're lookinf for.

UPDATED: The com.javax.transform packages should help. Take a look at the Transformer example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文