Tools‎ > ‎Java API‎ > ‎Documentation‎ > ‎

Dealing with JAXBElement

The PharmML DOM is partially generated using JAXB (see to convert the XML Schema into a set of Java classes. Each schema maps to a separate java package. The XML Schema design uses the "Garden of Eden" design pattern which means all elements have a simple of complex type. JAXB creates classes for most types and some of the elements, but often elements are defined by their type. For example to get the content of a <Description> element you will use a class corresponding to its type AnnotationType:

AnnotationType descn = anElement.getDescription();

Things get more complicated when we use substitution groups, which is effectively a form of inheritance. In this case we need to use the JAXBElement class, and then test what class to use. For example when accessing an element of the substitution group Scalar we must do the following to tech what was the 'inherited' class:

JAXBElement<?> scalar = rhs.getScalar();
    RealValueType real = (RealValueType)scalar.getValue();

In order to create a JAXBElement instance with a specified type, one should rely on the ObjectFactory classes. Each package has its own object factory for creating JAXBElement instances for the classes belonging to it. The following example describes how to add a new derivative variable to a structural model:

// Creating the object factory for common types
eu.ddmore.libpharmml.dom.commontypes.ObjectFactory of = new eu.ddmore.libpharmml.dom.commontypes.ObjectFactory();

// Creating the derivative variable itself
DerivativeVariable dv = new DerivativeVariable("Ac", SymbolType.REAL);
// Wrapping it into a new JAXBElement
JAXBElement<DerivativeVariable> jaxb_dv = of.createDerivativeVariable(dv);

// Adding it to the structural model

libPharmML is in a continuous development and a lot of efforts are made to remove the use of JAXBElement wrappers. The mechanism described in the last example can be simplified as follows:

// Will perform all the steps described previouslystructuralModel.createDerivativeVariable("Ac", SymbolType.REAL);