What is JAXB and why would I use it?

There is guy here swearing up and down that JAXB is the greatest thing since sliced bread. I am curious to see what stackoverflow users think the use case is for JAXB and what makes it a good or a bad solution for that case.

What is .net dynamic data and why would I use it?

What is .net dynamic data and why would I use it?

What is JSON and why would I use it?

I’ve looked on wikipedia and Googled it and read the official documentation, but I still haven’t got to the point where I really understand what JSON is, and why I’d use it. I have been building appl



Why would I use !! in JavaScript? [duplicate]

This question already has an answer here: What is the !! (not not) operator in JavaScript? 19 answers While reading lodash source code, I saw: this.__chain__ = !!chainAll; Why would one use !

Why would I use a Bound Service?

For communicating between the application and a Service, why would I use a Bound service rather than sending data in the Intent: mServiceIntent = new Intent(getActivity(), RSSPullService.class); mServ

Why would I use Reflection in a business app?

I know what Reflection is but why would I need to use it in a typical line-of-business Winforms app?

What does `std::kill_dependency` do, and why would I want to use it?

I’ve been reading about the new C++11 memory model and I’ve come upon the std::kill_dependency function (§29.3/14-15). I’m struggling to understand why I would ever want to use it. I found an example

What expression would I use? [closed]

What regular expression would I use for checking if a string contains at least 1 uppercase letter, 1 lowercase letter, a symbol, and a number? I am using PHP and JavaScript.

Why would I want to use Interfaces? [closed]

I understand that they force you to implement methods and such but what I cant understand is why you would want to use them. Can anybody give me a good example or explanation on why I would want to im

What to use instead validator in jaxb 2.0

JAXB version 1 spec have a Validator object, in version 2 Validator object was deprecated and optional. What do I need to use instead Validator object?

HTML5 custom attributes – Why would I use them?

I can’t seem to understand why should I be happy with HTML5 allowing custom attributes? Why would I use them?

Answers

It’s an “ORM for XML”. Most often used alongside JAX-WS (and indeed the Sun implementations are developed together) for WS Death Star systems.

With JAXB you can automatically create XML representations of your objects (marshalling) and object representations of the XML (unmarshalling).

As far as the XML Schema is concerned, you have two choices:

  • Generate Java classes from an XSD
  • Generate an XSD from your Java classes

There are also some simpler XML serialization libraries like XStream, Digester or XMLBeans that might be alternatives.

I’m a big fan of JAXB for manipulating XML. Basically, it provides a solution to this problem (I’m assuming familiarity with XML, Java data structures, and XML Schemas):

Working with XML is difficult. One needs a way to take an XML file – which is basically a text file – and convert it into some sort of data structure, which your program can then manipulate.

JAXB will take an XML Schema that you write and create a set of classes that correspond to that schema. The JAXB utilities will create the hierarchy of data structures for manipulating that XML.

JAXB can then be used to read an XML file, and then create instances of the generated classes – laden with the data from your XML. JAXB also does the reverse: takes java classes, and generates the corresponding XML.

I like JAXB because it is easy to use, and comes with Java 1.6 (if you are using 1.5, you can download the JAXB .jars.) The way it creates the class hierarchy is intuitive, and in my experience, does a decent job abstracting away the “XML” so that I can focus on “data”.

So to answer your question: I would expect that, for small XML files, JAXB might be overkill. It requires you to create and maintain an XML schema, and to use “standard textbook methods” of utilizing Java classes for data structures. (Main classes, small inner-classes to represent “nodes”, and a huge hierarchy of them.) So, JAXB is probably not that great for a simple linear list of “preferences” for an application.

But if you have a rather complex XML schema, and lots of data contained within it, then JAXB is fantastic. In my project, I was converting large amounts of data between binary (which was consumed by a C program) and XML (so that humans could consume and modify that data). The resulting XML Schema was nontrivial (many levels of hierarchy, some fields could be repeated, others could not) so JAXB was helpful in being able to manipulate that.

Here’s a reason not to use it: performance suffers. There is a good deal of overhead when marshaling and unmarshaling. You might also want to consider another API for XML-Object binding — such as JiBX: http://jibx.sourceforge.net/

JAXB is great if you have to code to some external XML spec defined as an XML schema (xsd).

For example, you have a trading application and you must report the trades to the Uber Lame Trade Reporting App and they’ve given you ultra.xsd to be getting on with. Use the $JAVA_HOME/bin/xjc compiler to turn the XML into a bunch of Java classes (e.g. UltraTrade).

Then you can just write a simple adapter layer to convert your trade objects to UltraTrades and use the JAXB to marshal the data across to Ultra-Corp. Much easier than messing about converting your trades into their XML format.

Where it all breaks down is when Ultra-Corp haven’t actually obeyed their own spec, and the trade price which they have down as a xsd:float should actually be expressed as a double!

I use JAXB at work all the time and I really love it. It’s perfect for complex XML schemas that are always changing and especially good for random access of tags in an XML file.

I hate to pimp but I just started a blog and this is literally the first thing I posted about!

Check it out here: http://blog.gonigberg.com/2010/04/21/getting-started-with-jaxb/

You can also check out JIBX too. It is also a very good xml data binder, which is also specialized in OTA (Open Travel Alliance) and is supported by AXIS2 servers. If you’re looking for performance and compatibility, you can check it out :

http://jibx.sourceforge.net/index.html

JAXB provides improved performance via default marshalling optimizations. JAXB defines a programmer API for reading and writing Java objects to and from XML documents, thus simplifying the reading and writing of XML via Java.

Why we need JAXB? The remote components (written in Java) of web services uses XML as a mean to exchange messages between each other. Why XML? Because XML is considered light weight option to exchange message on Networks with limited resources. So often we need to convert these XML documents into objects and vice versa. E.g: Simple Java POJO Employee can be used to send Employee data to remote component( also a Java programme).

class Employee{
 String name;
 String dept;
 ....
}

This Pojo should be converted (Marshall) in to XML document as follow:

<Employee>
  <Name>...</Name>
  <Department>...</Department>
</Employee>

And at the remote component, back to Java object from XML document (Un-Marshall).

What is JAXB?

JAXB is a library or a tool to perform this operation of Marshalling and UnMarshalling. It spares you from this headache, as simple as that.