Java 用于 XML 解析的 JAXB 的替代方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24838202/
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-08-14 15:05:52  来源:igfitidea点击:

Alternative to JAXB for XML parsing

javaxml-parsingjaxbstax

提问by Hector

I am currently using JAXB to parse XML documents, however i need a better performing XML processor.

我目前正在使用 JAXB 来解析 XML 文档,但是我需要一个性能更好的 XML 处理器。

Better = Faster and decrease memory footprint.

更好 = 更快并减少内存占用。

I have to process literally millions of separate XML documents.

我必须处理数百万个单独的 XML 文档。

I am using websphere application server v7 and java 6.

我正在使用 websphere 应用程序服务器 v7 和 java 6。

I have read Stax is the way to go via JAXP, but then i have seen articles saying JAXP is outdated.

我读过 Stax 是通过 JAXP 的方式,但后来我看到文章说 JAXP 已经过时了。

If this is true, what are my althernatives to effeciently process millions of XML doucments (each XML doc is beteen 5Kb - 10Kb) without causing my application servers to crash with memory issues.

如果这是真的,那么我有什么替代方法可以有效地处理数百万个 XML 文档(每个 XML 文档的大小为 5Kb - 10Kb)而不会导致我的应用程序服务器因内存问题而崩溃。

采纳答案by Mark Bramnik

I think first of all you should track the memory issues. How many of these XML are maintained in memory simultaneously, is it possible to keep only one (or at least some fairly small amount of XMLs) in memory simultaneously? On servers Java processes usually takes at least 1Gb of memory so its not really clear whether the XML parsing is something that makes you process fail.

我认为首先你应该跟踪内存问题。这些 XML 中有多少同时保存在内存中,是否可以同时在内存中只保存一个(或至少一些相当少量的 XML)?在服务器上,Java 进程通常至少需要 1Gb 的内存,因此不太清楚 XML 解析是否会导致您的进程失败。

So I really believe you should work with a profiler here, before coming to conclusions that the XML parser should be changed.

所以我真的相信你应该在这里使用分析器,然后才能得出应该更改 XML 解析器的结论。

There are a lot of parsers out there, You might try woodstoxwhich is a stax parser. Another option can be xstreamIf you are looking for something that resembles JAXB, you might want to give a try to a Simple XML parser

那里有很多解析器,您可以尝试使用 stax 解析器woodstox。另一个选项可以是xstream如果您正在寻找类似于 JAXB 的东西,您可能想尝试一个简单的 XML 解析器

Bottom line I believe you should first understand where does the issue exist, and if you resolve it, the chances are that you won't need to switch to another framework at all

底线我相信您应该首先了解问题存在于何处,如果您解决了它,那么您可能根本不需要切换到另一个框架

回答by Alex Punnen

You can use Groovy within Java to read xml. Create a Groovy class within your Java source dir if you are using maven

您可以在 Java 中使用 Groovy 来读取 xml。如果您使用的是 maven,请在您的 Java 源代码目录中创建一个 Groovy 类

src/main/groovy

源代码/主/常规

and use Groovy XMLParser to parser to parse or other class to write XML. It is much easier with Groovy to walk through the xml.

并使用 Groovy XMLParser 来解析或其他类来编写 XML。使用 Groovy 浏览 xml 会容易得多。

You can call the Groovy class as a Java class inside your Java program as Groovy compiles to Java class files

当 Groovy 编译为 Java 类文件时,您可以在 Java 程序中将 Groovy 类作为 Java 类调用

To do this via maven use

要通过 maven 执行此操作,请使用

<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<executions>
    <execution>
        <goals>
            <goal>generateStubs</goal>
            <goal>compile</goal>
            <goal>generateTestStubs</goal>
            <goal>testCompile</goal>
        </goals>
    </execution>
</executions>
</plugin>