How do you specify the date format used when JAXB marshals xsd:dateTime?

You can use an XmlAdapter to customize how a date type is written to XML. package com.example; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.bind.annotation.adapters.XmlAdapter; public class DateAdapter extends XmlAdapter<String, Date> { private final SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); @Override public String marshal(Date v) throws Exception { synchronized (dateFormat) { return dateFormat.format(v); } } @Override public … Read more

Using JAXB to cross reference XmlIDs from two XML files

This can be done with an XmlAdapter. The trick is the XmlAdapter will need to be initialized with all of the Nodes from Network.xml and passed to the Unmarshaller used with NetworkInputs.xml: import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = … Read more

How to express dependency in maven on java ee features for transition to Java 9?

The Module System speaks of the way the unnamed modules as in your case of loading the application from classpath constructs the module graph. Further, from the documentation itself:- When the compiler compiles code in the unnamed module, or the java launcher is invoked and the main class of the application is loaded from the … Read more

javax.xml.bind.JAXBException Implementation of JAXB-API has not been found on module path or classpath

Add these dependencies into your pom/gradle: Gradle: compile(‘javax.xml.bind:jaxb-api:2.3.0’) compile(‘javax.activation:activation:1.1’) compile(‘org.glassfish.jaxb:jaxb-runtime:2.3.0’) Pom: <!– https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api –> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0-b170201.1204</version> </dependency> <!– https://mvnrepository.com/artifact/javax.activation/activation –> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1</version> </dependency> <!– https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime –> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.0-b170127.1453</version> </dependency>

Generating a JAXB class that implements an interface

Unfortunately, it looks like the interface-injection plugin mentioned in some of the other answers is no longer well-supported. In fact, I’m having trouble finding the JAR for download. Thankfully, the JAXB2 Basics Plugins provides a similar mechanism for adding an interface to the generated JAXB stubs (see the Inheritance plugin). The Inheritance plugin documentation has … Read more

Using JAXB to unmarshal/marshal a List

I used @LiorH’s example and expanded it to: @XmlRootElement(name=”List”) public class JaxbList<T>{ protected List<T> list; public JaxbList(){} public JaxbList(List<T> list){ this.list=list; } @XmlElement(name=”Item”) public List<T> getList(){ return list; } } Note, that it uses generics so you can use it with other classes than String. Now, the application code is simply: @GET @Path(“/test2”) public JaxbList … Read more

Difference of Maven JAXB plugins

Let’s summarize. We have/had: the maven-jaxb2-plugin (https://github.com/highsource/maven-jaxb2-plugin) the maven-jaxb-plugin (https://jaxb.dev.java.net/jaxb-maven2-plugin/) the jaxb2-maven-plugin (https://github.com/mojohaus/jaxb2-maven-plugin) Based on the comments of this thread, I’ve always used the maven-jaxb2-plugin (i.e. plugin #1): Concerning the org.jvnet.jaxb2.maven2:maven-jaxb2-plugin versus com.sun.tools.xjc.maven2:maven-jaxb-plugin, from my point of view it’s definitely the first one (http://maven-jaxb2-plugin.java.net/). This plugin has much more features than com.sun.tools.xjc.maven2:maven-jaxb-plugin, the development is … Read more

JAXB marshalling XMPP stanzas

How about the following?: Create a custom XMLStreamWriter that will treat all namespace declarations as default namespaces, and then marshal to that: ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLOutputFactory xof = XMLOutputFactory.newFactory(); XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out); xsw = new MyXMLStreamWriter(xsw); m.marshal(iq, xsw); xsw.close(); MyXMLStreamWriter import java.util.Iterator; import javax.xml.namespace.NamespaceContext; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; public class MyXMLStreamWriter implements … Read more