jaxb tutorial

19
eBooks by

Upload: intertech-training

Post on 12-Apr-2017

31 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Jaxb Tutorial

eBooks by

JAXB Tutorial

Page 2: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 1

Table of Contents

1. How to Marshal and Unmarshal XML .......................................... 2

2. Customized Namespace Prefixes Example using NamespacePrefixMapper .............................................................. 10

a. Introduction ............................................................................................ 3 b. Spring’s Jaxb2Marshaller ...................................................................... 4 c. Java’s XML Packages ........................................................................... 6

a. Introduction ........................................................................................... 11 b. Dependencies ...................................................................................... 12 c. NamespacePrefixMapper ..................................................................... 13 d. Configure .............................................................................................. 16

Page 3: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 2

How to Marshal and

Unmarshal XML

Page 4: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 3

This JAXB tutorial describes how to use JAXB to marshal and unmarshal XML strings using either Spring or the standard javax.xml.bind interfaces.

The samples below are using XML strings instead of files, but could easily be adapted to files if needed.

Introduction

Page 5: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 4

Configuration To use Spring’s Jaxb2Marshaller, you will need the spring-oxm dependency, which you may already have, since it is used by other major spring components.

To configure the Jaxb2Marshaller in your Spring context, add the config XML below. Be sure to add all root packages that contain the JAXB classes.

Spring’s Jaxb2Marshaller

Page 6: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 5

Unmarshalling To unmarshal an XML string into a JAXB object, just inject the Jaxb2Marshaller and call the unmarshal() method.

Marshalling To marshal a JAXB object into an XML string, you will need to create a StringResult and pass that to the marshal() method.

Page 7: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 6

Java 6 and later have the standard JAXB interfaces and factories for processing xml available in the java.xml.bind package. You can find more info about JAXB on the JAXB project web site.

Unmarshalling To unmarshal an xml string into a JAXB object, you will need to create an Unmarshaller from the JAXBContext, then call the unmarshal() method with a source/reader and the expected root object.

Java’s XML Packages

JAXB project web site

Page 8: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 7

Page 9: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 8

Marshalling To marshal a JAXB object into an XML string, you will need to create a a Marshallerfrom the JAXBContext and pass a Writer target to the marshal() method.

Page 10: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 9

As you can see, the Spring mechanisms are pretty clean, but if you are not using Spring, the Java mechanisms are easy to work with as well.

Next up in this JAXB tutorial, we’ll see how to configure the namespace prefixes.

JAXB Tutorial Side Note If you’ve ever wondered about the correct spelling of marshalling (vs marshaling), there’s a brief summary on Stack Overflow… (2 l’s is acceptable) Stack Overflow

Page 11: Jaxb Tutorial

Customized Namespace

Prefixes Example using

NamespacePrefixMapper

www.intertech.com + 800.866.9884 @intertech.com 10

Page 12: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 11

Do you need to specify what namespace prefixes are used in your JAXB marshaled XML? For example, do you want to see <soap:envelope/> in the generated XML instead of <ns1:envelope/>? If so, this JAXB tutorial will walk you through those steps.

If you are using the default JAXB implementation provided with Java 6 or later, you can configure the namespace prefixes by extending the NamespacePrefixMapper and setting a property to tell the marshaller to use your extension.

Introduction

Page 13: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 12

Since you need to extend a class that is defined in the JAXB API’s Reference Implemenation (RI), you must pull it in as a dependency.

You can download the libraries directly from jaxb.java.net, or you can add the Maven dependencies.

Dependencies

jaxb.java.net

Page 14: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 13

Once you have the JAXB libraries, you can extend the com.sun.xml.bind.marshaller.NamespacePrefixMapper class. This is the core class that provides the mapping of XML namespaces to the desired prefix.

NamespacePrefixMapper

Page 15: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 14

Sample extension (using Java 8 syntax)

Page 16: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 15

Set the “com.sun.xml.bind.namespacePrefixMapper” property on the marshaller to an instance of your mapper. Once set, any time you marshal your JAXB object to XML, it will use your mapper to get the prefix for a given namespace.

Spring’s Jaxb2Marshaller To configure Spring’s Jaxb2Marshaller to use your NamespacePrefixMapper, you should set the marshallerProperties map on the Jaxb2Marshaller bean.

Configure

Page 17: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 16

Java’s Marshaller To configure the javax.xml.bind.Marshaller, you can set the property directly on the marshaller by calling marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new DefaultNamespacePrefixMapper());

Page 18: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 17

Example: (line 25)

Note: If you use the same mappings across your application, you can create one instance of the mapper and share it.

Page 19: Jaxb Tutorial

www.intertech.com + 800.866.9884 @intertech.com 18

Need help with a Java development project? Speak with a Java Consulting Representative today.

651.288.7001

See our list of Consulting Solutions.