5

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

abarisone
  • 3,707
  • 11
  • 35
  • 54
Guido Anselmi
  • 3,872
  • 8
  • 35
  • 58

5 Answers5

3

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:

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

Is it worth using JAXB for this?

Yes.

Dave Cassel
  • 8,352
  • 20
  • 38
bdoughan
  • 147,609
  • 23
  • 300
  • 400
2

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.

Community
  • 1
  • 1
Eric Bloch
  • 2,882
  • 2
  • 20
  • 26
1

XQSync uses XStream for this. As I understand it, JAXB is more powerful - but also more complex.

mblakele
  • 7,782
  • 27
  • 45
  • @mblakele - JAXB is more powerful. As far as complexity goes here is a side by side example demonstrating how to map the same object model to a known XML schema using both JAXB and XStream: http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-xstream.html. Note: I'm the EclipseLink JAXB (MOXy) lead. – bdoughan Oct 06 '11 at 10:23
1

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

0

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • @Mike: The Schema is simple but non-existent. This will be a side project of the main project. – Guido Anselmi Oct 06 '11 at 20:41
  • @Mike: I responded to the Java question in the body. Good question. – Guido Anselmi Oct 06 '11 at 20:51
  • @Mike: Oh? The 'developer of the Saxon XSLT processor' sees this problem as a nail for his hammer. Why Java or C#? Perhaps the design of the languages, their natural ontology, their syntactic features, the static typing, their readability and maintainability, their compiler services, their intellisense, autocompletion, reference highlighting/replacing, the IDE and editor, code colouring, error checking, static analysis, built-in comments and help system, even the spell-checker, callstack, step-through debugger, the call hierarchy, code visualization tools, shall I go on? – Luke Puplett Mar 21 '13 at 19:14
  • No, don't. I just hate to see people writing 100 lines of Java where 10 lines of XQuery or XSLT would do the job, for the sole reason that they enjoy using their familiar Java skills and tools. As I said, in projects I have worked with, JAXB has worked OK when the schema has been reasonably simple and very stable; it's been a disaster where the schema is complex or frequently changing. And I didn't rule out using Java, I just asked whether there was a good rationale for the choice. – Michael Kay Mar 22 '13 at 09:35