Home » Tag Archives: JAXB

Tag Archives: JAXB

Generating JSON Schema from XSD with JAXB and Jackson

json-logo

In this post, I demonstrate one approach for generating JSON Schema from an XML Schema (XSD). While providing an overview of an approach for creating JSON Schema from XML Schema, this post also demonstrates use of a JAXB implementation (xjc version 2.2.12-b150331.1824 bundled with JDK 9 [build 1.9.0-ea-b68]) and of a JSON/Java binding implementation (Jackson 2.5.4). The steps of this ...

Read More »

用于Java和XML绑定的JAXB教程– 终极指南

java-logo

Java为处理XML结构和文件提供了一系列的选项。JAXB是其中最常见使用的一种。JAXB全称是Java Architecture for XML Binding。它能够将Java对象装换为XML结构,反过来亦然。JAXB是在JRE 1.6的第一个版本中随着JRE标准包一起发布的。 JAXB的第一个规范在2003年3月完成,实现过程由 Java Specification Request 31: https://jcp.org/en/jsr/detail?id=31所跟踪。在这个规范请求中你可发现很多关于JAXB的成长和所有的改进。 就像刚才提到的,JAXB实在1.6中加入JRE包的。在此之前,要使用它必须将它的包加入到工程中。 在JAXB可以使用之前(很久之前),Java处理XML的方式是DOM: http://www.w3.org/DOM/。这种方式并不是最好的,因为没有很好的抽象来映射XML节点,所有的值类型都被认为是字符串。JAXB有许多的优点,比如面向对象方式的操作XML节点和属性,值类型,注解和本文中将要阐述的其他优点。 本文中的所有例子程序都是使用以下版本来实现的: JRE 1.8.0 for 32b。所使用的IDE是Eclipse SDK 版本: Luna (4.4)。但是其他所有包含JAXB API的Java版本和IDE也能够完美的工作,只要是基于Java 8的。 目录 1. 映射(Mapping) 2. 编组(Marshal) 3. 反编组(Un-marshal) 4. 适配器(Adapters) 5. XSDs 6. 注解 7. 工具 8. 最佳实践 9. 总结 10. 资源 11. 下载   1. 映射(Mapping) 使用一些注解和以下的特定的规则,Java对象就能和XML结构绑定起来。这就是我们称之为映射的原因。在本教程中,我们将使用例子、资源和其他的一些信息来解释以下几点。 我们将展示一些关于如何将Java对象转换为XML结构的例子程序, 这种转换被称为编组(marshaling)。我们将展示如何使用适配器(adapter)处理原始类型,集合和更加复杂的类型。 我们还会解释如何进行相反的操作,也就是反编组(un-marshaling),例如将XML文件转换为Java对象。 所有这些都是通过使用Java注解来完成的。我们会列举并解释和JAXB一起使用的最重要的注解。 我们还会介绍用来校验的XSD(XML Schema),这也是JAXB所支持的一个重要的工具。我们也会看一下XSD在编组时如何起作用。 最后,我们会列举一些与JAXB一起配合使用的工具,它们能在很多的方面都对程序员起到帮助。 ...

Read More »

JAXB Is Doing It Wrong; Try Xembly

java-logo

JAXB is a 10-year-old Java technology that allows us to convert a Java object into an XML document (marshalling) and back (unmarshalling). This technology is based on setters and getters and, in my opinion, violates key principles of object-oriented programming by turning objects into passive data structures. I would recommend you use Xembly instead for marshalling Java objects into XML ...

Read More »

The JAXB Well Known Secret

java-interview-questions-answers

Introduction I rediscovered a library that Java offers to the masses. When I first read the specification, I was confused and thought I needed all these special tools to implement. I found recently that all was needed was some annotations and a POJO. JAXB JAXB stands for Java Architecture for XML Binding. This architecture allows a developer to turn the ...

Read More »

JAXB – A Newcomer’s Perspective, Part 2

java-logo

In Part 1 of this series, I discussed the basics of loading data from an XML file into a database using JAXB and JPA. (If JSON is called for instead of XML, then the same idea should translate to a tool like Jackson.) The approach is to use shared domain objects – i.e. a single set of POJOs with annotations ...

Read More »

JAXB – A Newcomer’s Perspective, Part 1

java-logo

I know what a lot of you are already thinking, so let’s get this out of the way: “JAXB? As in XML? Come on, all the cool kids are using JSON.” The “XML vs. JSON” debate and the many arguments that contribute to it are pretty well documented; I won’t spend a lot of time rehashing them here. I believe ...

Read More »

Generate your JAXB classes in a second with xjc

java-interview-questions-answers

Since JAXB is part of the JDK, it is one of the most often used frameworks to process XML documents. It provides a comfortable way to retrieve and store data from XML documents to Java classes. As nearly every Java developer has already used JAXB, I will not explain the different JAXB annotations. Instead I will focus on a little ...

Read More »

Custom Spring namespaces made easier with JAXB

spring-interview-questions-answers

First of all, let me tell this out loud: Spring is no longer XML-heavy. As a matter of fact you can write Spring applications these days with minimal or no XML at all, using plenty of annotations, Java configuration and Spring Boot. Seriously stop ranting about Spring and XML, it’s the thing the of the past. That being said you ...

Read More »
Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.