–add-modules only on compilation [duplicate]

I made this answer a while ago where I answered this as an additional info to exposing non java.se packages in Java-9 using Maven. The added part specifically focusses on using the standalone version of the java.xml.* APIs. To adapt to which you can probably start consuming the dependency on jaxb-api:2.3.0 which can be loaded … Read more

JAXB and constructors

JAXB can support this case using an XML Adapter. Consider you have the following object with no zero-arg constructor: package blog.immutable; public class Customer { private final String name; private final Address address; public Customer(String name, Address address) { this.name = name; this.address = address; } public String getName() { return name; } public Address … Read more

What is the difference between jaxb-impl and jaxb-runtime?

The only difference between jaxb-impl and jaxb-runtime is packaging: jaxb-impl bundles istack/txw2 inside the jar, whereas jaxb-runtime provides them via separate dependencies. Version Compatibility and the JakartaEE Migration I’ve been trying to make sense of this for the last day, and it’s incredibly confusing. Particularly when you’re trying to avoid the java.xml.bind to jakarta.xml.bind migration. … Read more

Java/JAXB: Unmarshall Xml to specific subclass based on an attribute

JAXB is a spec, specific implementations will provide extension points to do things such as this. If you are using EclipseLink JAXB (MOXy) you could modify the Shape class as follows: import javax.xml.bind.annotation.XmlAttribute; import org.eclipse.persistence.oxm.annotations.XmlCustomizer; @XmlCustomizer(ShapeCustomizer.class) public abstract class Shape { int points; @XmlAttribute public int getPoints() { return points; } public void setPoints(int points) … Read more

JAXB Marshalling Unmarshalling with CDATA

You could do the following: AdapterCDATA package forum14193944; import javax.xml.bind.annotation.adapters.XmlAdapter; public class AdapterCDATA extends XmlAdapter<String, String> { @Override public String marshal(String arg0) throws Exception { return “<![CDATA[” + arg0 + “]]>”; } @Override public String unmarshal(String arg0) throws Exception { return arg0; } } Root The @XmlJavaTypeAdapter annotation is used to specify that the XmlAdapter … Read more

Marshalling LocalDate using JAXB

You will have to create an XmlAdapter like this: public class LocalDateAdapter extends XmlAdapter<String, LocalDate> { public LocalDate unmarshal(String v) throws Exception { return LocalDate.parse(v); } public String marshal(LocalDate v) throws Exception { return v.toString(); } } And annotate your field using @XmlJavaTypeAdapter(value = LocalDateAdapter.class) See also javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters if you want to define your adapters … Read more

How to get formatted xml output from jaxb in spring?

<bean class=”org.springframework.oxm.jaxb.Jaxb2Marshaller”> <property name=”classesToBeBound”> <list> …. </list> </property> <property name=”marshallerProperties”> <map> <entry> <key> <util:constant static-field=”javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT” /> </key> <value type=”java.lang.Boolean”>true</value> </entry> </map> </property> </bean>

JAXB: How should I marshal complex nested data structures?

I’ve solved the problem without XmlAdapter’s. I’ve written JAXB-annotated objects for Map, Map.Entry and Collection. The main idea is inside the method xmlizeNestedStructure(…): Take a look at the code: public final class Adapters { private Adapters() { } public static Class<?>[] getXmlClasses() { return new Class<?>[]{ XMap.class, XEntry.class, XCollection.class, XCount.class }; } public static Object … Read more

when does JAXB unmarshaller.unmarshal returns a JAXBElement or a MySchemaObject?

If the root element uniquely corresponds to a Java class then an instance of that class will be returned, and if not a JAXBElement will be returned. If you want to ensure that you always get an instance of the domain object you can leverage the JAXBInstrospector. Below is an example. Demo package forum10243679; import … Read more

JAXB required=true doesn’t seem to require

The JAXB reference implementation doesn’t use this attribute for validation, it’s purely there for documentation purposes. If you need to validate the documents, you need to define an XML Schema, and inject it into the Marshaller or Unmarshaller, using SchemaFactory.