Server-Side XML Validation with CXF Webservice

You can override validation error messages, inserting a line number, by using a custom ValidationEventHandler: package example; import javax.xml.bind.ValidationEvent; import javax.xml.bind.helpers.DefaultValidationEventHandler; public class MyValidationEventHandler extends DefaultValidationEventHandler { @Override public boolean handleEvent(ValidationEvent event) { if (event.getSeverity() == ValidationEvent.WARNING) { return super.handleEvent(event); } else { throw new RuntimeException(event.getMessage() + ” [line:”+event.getLocator().getLineNumber()+”]”); } } } If you configure … Read more

How to generate JAXB classes from just XML

There are many tools available (a quick google search should fetch you some) that can generate XSD from XML assuming string type for almost everything. You should be able to use that XSD to run JAXB to get classes. Here’s an online tool that lets you do that. And here is a screen cap:

Proper fix for Java 10 complaining about illegal reflection access by jaxb-impl 2.3.0?

jaxb-ri runtime uses ClassLoader#defineClass / Unsafe#defineClass to do some bytecode modification in runtime to optimize performance. ClassLoader#defineClass is tried first which causes the warning. This legacy optimization is removed completely in jaxb-ri master (after 2.3.0, not released yet). To disable this optimization for 2.3.0, set system property com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize. After next jaxb-ri release updating to newest … Read more

What is JAXB and why would I use it? [closed]

I’m a big fan of JAXB for manipulating XML. Basically, it provides a solution to this problem (I’m assuming familiarity with XML, Java data structures, and XML Schemas): Working with XML is difficult. One needs a way to take an XML file – which is basically a text file – and convert it into some … Read more

How to marshall a string using JAXB that sometimes contains XML content and sometimes does not?

Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group. This use case is mapped using the @XmlAnyElement annotation and specifying a DOMHandler. There appears to be bug when doing this with the JAXB RI, but the following use case works with EclipseLink JAXB (MOXy). BodyDomHandler By default a … Read more

Using JAXB to extract inner text of XML element

You can use the @XmlAnyElement annotation as described by bmargulies. To map to the object model in your question you can leverage a DOMHandler. Main import javax.xml.bind.annotation.*; @XmlRootElement(name=”main”) @XmlAccessorType(XmlAccessType.FIELD) public class Main { private String name; private Integer maxInstances; @XmlAnyElement(value=ParameterHandler.class) private String parameters; } ParameterHandler import java.io.*; import javax.xml.bind.ValidationEventHandler; import javax.xml.bind.annotation.DomHandler; import javax.xml.transform.Source; import javax.xml.transform.stream.*; … Read more

How to prevent JAXB escaping a string

By default, the marshaller implementation of the JAXB usually escapes characters. To change this default behavior you will have to Write a class that implements the com.sun.xml.bind.marshaller.CharacterEscapeHandler interface. set that handler in the Marshaller CharacterEscapeHandler escapeHandler = NoEscapeHandler.theInstance; marshaller.setProperty(“com.sun.xml.bind.characterEscapeHandler”, escapeHandler); You can have a look at samples provided with JAXB, character escaping sample

JAXB annotations for nested element lists

By default JAXB (JSR-222) implementations consider public properties (get/set methods) and annotated fields as mapped (and separate). The default mapping is @XmlElement so your properties will be considered as mapped this way. Solution #1 Since you are annotating the fields you need to add @XmlAccessorType(XmlAccessType.FIELD) on your classes. @XmlAccessorType(XmlAccessType.FIELD) public class Parameter { @XmlAttribute(name = … Read more