user1079293
user1079293

Reputation:

Replacing XStream with JAXB in my application

Currently in the application we get a SOAP response xml with namespace attached to each element. I have hard coded into my code that the namespaces are ignored because the application wants clean xml. But upon testing it is being deemed a weak solution because the namespace might change in the future. It has been recommended to me to use jaxb. I am using xtream currently because we can directly read xml. So I have started looking into jaxb. But jaxb requires xsd. I have understood the process but I am not sure how to implement Jaxb in my application because I am getting an xml response. So, my question Is it possible to replace xtream with jaxb in this situation?
thanks

Upvotes: 2

Views: 4936

Answers (1)

bdoughan
bdoughan

Reputation: 149017

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

I am using xtream currently because we can directly read xml. So I have started looking into jaxb. But jaxb requires xsd.

It is a common misconception that JAXB requires an XML schema (it's even on the XStream FAQ). The truth is that JAXB was designed to start from objects and provides an option to generate annotated object models from an XML schema(s). This is very useful when dealing with large XML schemas:

Currently in the application we get a SOAP response xml with namespace attached to each element. I have hard coded into my code that the namespaces are ignored because the application wants clean xml. But upon testing it is being deemed a weak solution because the namespace might change in the future.

JAXB has very good support for mapping namespaces. This can be done at the package level with @XmlSchema, the class level with @XmlType, or the field/property level with @XmlAttribute/@XmlElement:

I have understood the process but I am not sure how to implement Jaxb in my application because I am getting an xml response

Below is a link to an article that maps the same object model to the same XML document using both JAXB and XStream. It will give you a feel for how some of the concepts relate:

Another thing note is that JAXB refers to a standard (JSR-222) and not a specific implementation. There are actually several implementations including:

  • EclipseLink MOXy
  • Metro (the reference implementation)
  • Apache JaxMe

Upvotes: 6

Related Questions