开发者

MarkLogic to Java & Back Solution

开发者 https://www.devze.com 2023-04-10 19:44 出处:网络
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:

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:

  1. 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?)
  2. Should I process the XML myself using one of the XML APIs or is there a simpler way?
  3. Is it worth using JAXB for this?

Someone asked a good question of why I am using Java. I am using Jav开发者_运维技巧a/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.


Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.

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?)

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:

  • http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html
  • http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html

Should I process the XML myself using one of the XML APIs or is there a simpler way?

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).

  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

Is it worth using JAXB for this?

Yes.


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 uses XStream for this. As I understand it, JAXB is more powerful - but also more complex.


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).


Does using MarkLogic have any impact on the XML technology stack?

No. By the time it comes out of MarkLogic, it's just XML that could have come from anywhere.

I need to query XML and marshal it into Java objects.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消