如何从XML反序列化Java对象? [英] How to deserialize Java objects from XML?

查看:131
本文介绍了如何从XML反序列化Java对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这可能已经详细讨论或之前已经回答过,但我需要更多关于我的情况的最佳方法的信息...

I'm sure this might have been discussed at length or answered before, however I need a bit more information on the best approach for my situation...

问题:

我们需要将一些大型XML数据(从100k到5MB)扩展到Java对象中。问题是数据根本不能很好地映射到对象上,所以我们只需要拉出数据的某些部分并创建对象。鉴于此,JAXB或XStream等解决方案确实不合适。

Problem:
We have some large XML data (anywhere from 100k to 5MB) which we need to inflate into Java objects. The issue is that the data doesn't really doesn't map onto an object very well at all, so we need to only pull certain parts of the data out and create the objects. Given that, solutions such as JAXB or XStream really aren't appropriate.

因此,我们需要提取XML数据并尽可能高效地将其转换为java对象。

So, we need to pull XML data out and get it into java objects as efficiently as possible.



可能的解决方案:

我看到它的方式,我们有3个可能的解决方案:


Possible Solutions:
The way I see it, we have 3 possible solutions:


  • SAX解析

  • DOM解析

  • XSLT

我们可以将XML加载到任何JAXP实现中,并使用上述方法之一拉出数据。

We can load the XML into any JAXP implementation and pull the data out using one of the above methods.



问题

我有几个问题/顾虑:


Question(s)
I have a few questions/concerns:


  • XSLT如何在幕后工作?它只是一个DOM解析器吗?我问,因为XSLT似乎是一个很好的方法,但我真的不想考虑它,如果它不会给我们比DOM更好的性能。

  • 什么是一些流行的库提供DOM,XSLT和SAX XML解析器?

  • 根据您的经验,选择DOM,SAX或XSLT的原因是什么? DOM或XSLT的易用性是否完全支配SAX提供的性能改进?

  • 那里有任何基准吗?我发现的那些是旧的(如8岁)。因此,最近的一些基准测试将会受到赞赏。

  • 除了上面列出的那些我还有什么其他解决方案可能会丢失吗?

  • How does XSLT work under the hood? Is it just a DOM parser? I ask because XSLT seems like a good way to go, but I don't really want to consider it if it won't give us better performance than DOM.
  • What are some popular libraries that provide DOM, XSLT, and SAX XML parsers?
  • In your experience, what are the reasons for picking DOM, SAX, or XSLT? Does the ease of use of DOM or XSLT totally dominate the performance improvements SAX offers?
  • Any benchmarks out there? The ones I've found are old (as in, 8 years old). So some recent benchmarks would be appreciated.
  • Are there any other solutions besides those outlined above that I could be missing?



编辑:

一些说明......您可以使用XSLT直接将值注入Java对象...它通常用于将XML转换为其他XML,但我是从调用XSLT中的方法到java来注入值的观点。



我还不清楚XSLT处理器是如何正常工作的...它如何将XML提供给你编写的XSLT代码?



A few clarifications... You can use XSLT to directly inject values into a Java object... it is normally used to transform XML into some other XML, however I'm talking from the standpoint of calling a method from XSLT into java to inject the value.

I'm still not clear on how an XSLT processor works exactly... How is it feeding the XML into the XSLT code you write?

推荐答案

使用XSLT将大型XML文件转换为使用JAXB映射到java对象的本地域模型。

Use XSLT to transform the large XML files into a local domain model that is mapped to java objets with JAXB.

从内置XML库的JDK 5+开始(除非你绝对需要XSLT 2.0,在这种情况下使用Saxon)

Start with the JDK 5+ built in XML libraries (unless you absolutely need XSLT 2.0, in which case use Saxon)

不要专注于SAX / DOM的相对性能,专注于学习如何编写XPath表达式和使用XSLT,然后当且仅当您发现它是一个问题时才会担心性能。

Don't focus on relative performance of SAX/DOM, focus on learning how to write XPath expressions and use XSLT, and then worry about performance later if and only if you find it to be a problem.

Eclipse XML编辑器很不错,但如果你能负担得起,可以选择Oxygen XML,它可以让你实时进行XPath评估。

The Eclipse XML editors are decent, but if you can afford it, spring for Oxygen XML, which will let you do XPath evaluation in realtime.

这篇关于如何从XML反序列化Java对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆