我需要从 MarkLogic 服务器中查询 XML
并将其编组到 Java 对象中。解决这个问题的好方法是什么?具体来说:
- 使用MarkLogic对XML技术栈有什么影响吗? (即,MarkLogic 是否存在某种导致搜索、读取和写入 XML 片段的不同方法?)
- 我应该使用其中一个 XML API 自己处理 XML 还是有更简单的方法?
- 为此值得使用 JAXB 吗?
有人问了一个很好的问题,为什么我要使用 Java。我使用 Java/Java EE 因为我最擅长该语言。这是一个单人项目,我不想陷入任何困境。该项目旨在开发 Web 服务 API 以及数据处理和转换(CSV 到 XML)功能。 Java/Java EE 可以很好地做到这一点,而且做得很优雅。
I need to query XML
out of a MarkLogic server and marshal it into Java objects. What is a good way to go about this? Specifically:
- Does using MarkLogic have any impact on the XML technology stack? (i.e. is there something about MarkLogic that leads to a different approach to searching for, reading and writing XML snippets?)
- Should I process the XML myself using one of the XML APIs or is there a simpler way?
- Is it worth using JAXB for this?
Someone asked a good question of why I am using Java. I am using Java/Java EE because I am strongest in that language. This is a one man project and I don't want to get stuck anywhere. The project is to develop web service APIs and data processing and transformation (CSV to XML) functionality. Java/Java EE can do this well and do it elegantly.
发布评论
评论(5)
有许多XML-> Java 对象编组库。我想您可能想通过搜索通用的 Java XML 编组/解组问题来寻找这个问题的答案,如下所示:
Java 绑定与手动定义类
尽管标题编辑有所帮助,但您的用例仍然不完全清楚 - 如果您正在寻找 Java 连接性,您可能还需要查看 http://developer.marklogic.com/code/mljam 它允许您执行来自 MarkLogic XQuery 中的 Java 代码。
There are a number of XML-> Java object marshall-ing libraries. I think you might want to look for an answer to this question by searching for generic Java XML marshalling/unmarshalling questions like this one:
Java Binding Vs Manually Defining Classes
Your use case is still not perfectly clear although the title edit helps - If you're looking for Java connectivity, you might also want to look at http://developer.marklogic.com/code/mljam which allows you to execute Java code from within MarkLogic XQuery.
XQSync 使用
XQSync uses XStream for this. As I understand it, JAXB is more powerful - but also more complex.
使用 JAXB 来解组 XQuery 提供的 xml 已经有 5 年了,我不得不说我发现它非常有用并且节省时间。至于复杂性,它很容易学习和使用,大约 90% 的用途都是如此。我已经将它用于简单和复杂的模式,发现它非常高效且节省时间。从 MarkLogic 中执行 Java 代码通常是不可能的,因为它在 Marklogic 服务器上的单独虚拟机中运行,因此它实际上无法利用 Java EE Web 应用程序中的任何会话状态或库。使用 JAXB,可以非常轻松地获取结果流并将其转换为 Java 对象。我真的不能说太多关于它的好话。它使我的开发工作变得无比轻松,并允许您利用 Java 来做它最擅长的事情(跨各种技术和平台的丰富集成、高级业务逻辑、针对繁重处理作业的快速内存管理等),同时仍然使用 XQuery它最擅长什么(即搜索和转换内容)。
Having used JAXB to unmarshal xml served from XQuery for 5 years now, I have to say that I have found it to be exceptionally useful and time-saving. As for complexity, it is easy to learn and use for probably 90% of what you would be using it for. I've used it for both simple and complex schemas and found it to be very performant and time-saving. Executing Java code from within MarkLogic is usually a non-starter, because it runs in a separate VM on the Marklogic server, so it really can't leverage any session state or libraries from, say, a Java EE web application. With JAXB, it is very easy to take a result stream and convert it to Java objects. I really can't say enough good things about it. It has made my development efforts infinitely easier and allows you to leverage Java for those things that it does best (rich integration across various technologies and platforms, advanced business logic, fast memory management for heavy processing jobs, etc.) while still using XQuery for what it does best (i.e. searching and transforming content).
不。当它从 MarkLogic 中出来时,它只是可以来自任何地方的 XML。
为什么?
如果您有充分的理由使用 Java,那么我们需要知道该理由是什么,然后才能告诉您哪种 Java 技术适合。
如果您没有充分的理由使用 Java,那么您最好使用高级 XML 处理语言,例如 XSLT 或 XQuery。
至于 JAXB,当您的模式相当简单且稳定时,它是合适的。如果模式很复杂(例如学术期刊中文章的模式),那么由于生成的类数量较多,JAXB 可能会非常笨重。使用它来处理 XQuery 输出的一个问题是,XQuery 输出很可能不符合任何已知的架构,并且对于写入的每个查询,XQuery 结果的结构将有所不同。
No. By the time it comes out of MarkLogic, it's just XML that could have come from anywhere.
Why?
If you have a good reason for using Java, then we need to know what that reason is before we can tell you which Java technology is appropriate.
If you don't have a good reason for using Java, then you are better off using a high-level XML processing language such as XSLT or XQuery.
As for JAXB, it is appropriate when your schema is reasonably simple and stable. If the schema is complex (e.g. the schema for articles in an academic journal), then JAXB can be hopelessly unwieldy because of the number of classes that are generated. One problem with using it to process XQuery output is that it's very likely the XQuery output will not conform to any known schema, and the structure of the XQuery results will be different for each query that gets written.
注意:我是EclipseLink JAXB (MOXy) 领导者,以及 JAXB 2 成员(JSR-222)专家组。
有可能。一些对象到 XML 库比其他库支持更多种类的文档。 MOXy 利用基于 XPath 的映射,使其能够处理更广泛的文档。以下是一些示例:
使用框架通常更容易。 Java SE 提供了许多用于处理 XML 的标准库: JAXB (javax.xml.bind), XPath (javax.xml.xpath)、DOM、SAX、StAX。由于这些标准还有其他实现(即 MOXy 和 Apache JaxMe 实现 JAXB)。
是的。
Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.
Potentially. Some object-to-XML libraries support a larger variety of documents than other ones do. MOXy leverages XPath based mappings that allows it to handle a wider variety of documents. Below are some examples:
Using a framework is generally easier. Java SE offers may standard libraries for processing XML: JAXB (javax.xml.bind), XPath (javax.xml.xpath), DOM, SAX, StAX. Since these standards there are also other implementations (i.e. MOXy and Apache JaxMe implement JAXB).
Yes.